// AUSWERT.C - Utility-programme for Five-Nine
// Written by Stefan Pfeiffer, DL1ELY, 940812
// Last modification: 960222
// (C) 1994 by Stefan Pfeiffer, DL1ELY@DB0PKE

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <graphics.h>
#include <time.h>
#include <dos.h>
#include "auswert.h"

unsigned char textcol;
unsigned char titlecol;
unsigned char invcol;
unsigned char menucol;
unsigned char menuinvcol;
unsigned char activebuttoncol;
unsigned char passivebuttoncol;

unsigned char always_date;
unsigned char always_hour;
                
unsigned char do_blink;
unsigned char do_bar_stat;
unsigned char printport;
char logdir[128];

unsigned char svgamode;
unsigned char cntname[81];

unsigned char def_call[20];
unsigned char def_loc[7];
char fn_path[128];
char defname[128];
char date[20];
char title[80];
struct definition *act_def;

unsigned char pages;

struct index huge *ind;
unsigned int ind_cnt, ind_max, ind_seg;

uchar printer_type;

void disp_cnt_data()
{
   struct definition *def;
   char i;

   dfill(0,lastline-8,lastrow,lastline-1,' ',textcol);

   if(!bands)
   {
      dfill(0,lastline-4,lastrow,lastline-4,196,textcol);
      if(date[0]) dprintf(1,lastline-3,textcol,"%s, %s",title,date);
      dprintf(1,lastline-2,textcol,"[%s]",defname);
      dprint(1,lastline-1,textcol,"<No band defined>");
      return;
   };

   dfill(0,lastline-bands-3,lastrow,lastline-bands-3,196,textcol);
   dprintf(1,lastline-bands-2,textcol,"%s, %s",title,date);
   dprintf(1,lastline-bands-1,textcol,"[%s]",defname);
   

   i = 0;
   for(def=firstdef;def;def=def->next)
   {
      dprintf(1,lastline-bands+i,textcol,"%-8s:",def->band);
      if(def->found)
      {
         read_contest_info(def->fname);
         dprintf(11,lastline-bands+i,textcol,"%6ld points,",h.points);
         dprintf(25,lastline-bands+i,textcol,"%4d QSO%c,",h.qso_cnt,h.qso_cnt!=1?'s':' ');
         dprintf(35,lastline-bands+i,textcol," %2d %s,",h.countries,h.countries!=1?"Cnts.":"Cnt.");

         dprintf(46,lastline-bands+i,textcol,"%3d Square%c",h.squares,h.squares!=1?'s':' ');
         dprintf(59,lastline-bands+i,textcol,"(%s %s)",h.owncall,h.ownloc);
      }
      else dprintf(11,lastline-bands+i,textcol,"<Not found: %s>",def->fname);
      i++;
   };
};

unsigned create_new_contest(pchar fname,pchar call, pchar locator, pchar title)
{
   FILE *f;

  
   if((f = fopen(fname,"wb")) == NULL) return(0);
  
   memset((void *)&h,0,HEADER_SIZE);
    
   h.magic[0] = 'C';
   h.magic[1] = 'N';
   h.magic[2] = 'T';
   h.magic[3] = '2';
   h.magic[4] = 26;
    
   strcpy(h.owncall,call);
   strcpy(h.ownloc,locator);
   strcpy(h.desc,title);
      
   fwrite((void *)&h,HEADER_SIZE,1,f);
      
   fclose(f);
  
   return(1);
};



// make_path legt rekursiv den angegebenen DOS-Pfad an.
void make_path(char *dir)
{
   char *cp;

   if(chdir(dir))
   {
      cp = strrchr(dir,'\\');
      if(!cp) return;
      *cp = 0;
      make_path(dir);
      *cp = '\\';
      mkdir(dir);
      chdir(dir);
   };
};

 
void make_contest_file()
{
   void *save;

   char tag[4];
   char monat[4];
   char jahr[4];

   char call[14];
   char ncall[14];
   char dummy[5];
   char *cp;

   char result;
   char sel;

   char *mon_name[13] = {"","JAN","FEB","MAR","APR","MAI","JUN","JUL","AUG","SEP","OKT","NOV","DEZ"};
   char dir[128];
   char fname[128];
   char titel[40];
   char s[128];
   unsigned char hlp;
   struct date *d;
   FILE *f;
   unsigned char ch;


   getdate(d);
   sprintf(tag,"%02d\0",d->da_day);
   sprintf(monat,"%02d\0",d->da_mon);
   sprintf(jahr,"%02d\0",d->da_year%100);

   save = make_win(19,4,61,20,menucol);

   dprint(29,5,menucol,"  Create new contest  ");

   dprint(32,7,menucol,"Date: ");
   dprint(39,7,menucol,"  .  .");

   dprint(39,7,menuinvcol,tag);
   dprint(39,8,menucol,"DD");
   
   dprint(42,7,menuinvcol,monat);
   dprint(42,8,menucol,"MM");

   dprint(45,7,menuinvcol,jahr);
   dprint(45,8,menucol,"YY");

   dprint(30,10,menucol,"Call:");
   dfill(36,10,48,10,' ',menuinvcol);
   splitcall(def_call,dummy,call,dummy);
   dprint(36,10,menuinvcol,call);

   dprint(22,12,menucol,"Contest title:");
   dfill(22,13,58,13,' ',menuinvcol);

   sprintf(fname,"%s\\%s%02d\\%s.DES",logdir,mon_name[d->da_mon],d->da_year%100,call);
   dprint(22,15,menucol,"File: ");
   dfill(22,16,58,16,' ',menuinvcol);
   dprint(22,16,menuinvcol,fname);

   paint_but(25,18,"OK",passivebuttoncol,menucol);
   paint_but(41,18,"Abort",passivebuttoncol,menucol);

   titel[0] = 0;

   sel = 1;
   do
   {
      switch(sel)
      {
         case 1:
            result = input_string(39,7,2,2,INSERT|DO_TAB|DIGITS_ONLY,menuinvcol,tag);
            hlp = atoi(tag);
            if((!hlp)||(hlp>31)) hlp = d->da_day;

            sprintf(tag,"%02d\0",hlp);
            dprint(39,7,menuinvcol,tag);
            break;
         case 2:
            result = input_string(42,7,2,2,INSERT|DO_TAB|DIGITS_ONLY,menuinvcol,monat);
            hlp = atoi(monat);
            if((!hlp)||(hlp>12)) hlp = d->da_mon;

            sprintf(monat,"%02d\0",hlp);
            dprint(42,7,menuinvcol,monat);

            sprintf(fname,"%s\\%s%02d\\%s.DES",logdir,mon_name[hlp],atoi(jahr),call);
            dfill(22,16,58,16,' ',menuinvcol);
            dprint(22,16,menuinvcol,fname);

            break;
         case 3:
            result = input_string(45,7,2,2,INSERT|DO_TAB|DIGITS_ONLY,menuinvcol,jahr);
            hlp = atoi(jahr);
            sprintf(jahr,"%02d\0",hlp);
            dprint(45,7,menuinvcol,jahr);

            sprintf(fname,"%s\\%s%02d\\%s.DES",logdir,mon_name[atoi(monat)],hlp,call);
            dfill(22,16,58,16,' ',menuinvcol);
            dprint(22,16,menuinvcol,fname);

            break;
         case 4:
            result = input_string(36,10,12,12,INSERT|DO_TAB|UPCASE,menuinvcol,call);
            dfill(36,10,48,10,' ',menuinvcol);
            splitcall(call,dummy,ncall,dummy);
            strcpy(call,ncall);
            dprint(36,10,menuinvcol,call);

            sprintf(fname,"%s\\%s%02d\\%s.DES",logdir,mon_name[atoi(monat)],atoi(jahr),call);
            dfill(22,16,58,16,' ',menuinvcol);
            dprint(22,16,menuinvcol,fname);

            break;
         case 5:
            result = input_string(22,13,36,36,INSERT|DO_TAB,menuinvcol,titel);
            break;
         case 6:
            result = input_string(22,16,36,36,INSERT|DO_TAB|UPCASE,menuinvcol,fname);
            break;
         case 7:
            my_gotoxy(lastrow+1,lastline+1);
            paint_but(25,18,"OK",activebuttoncol,menucol);
            ch = getch();

            if(ch == 13)
            {
               if(!call[0])
               {
                  my_gotoxy(80,lastline+1);
                  ok_box("Warning","You must enter the call you want to use!");
                  result = TAB;
                  sel = 3;
               }
               else result = 2;
            };

            if(ch == 27) result = 0;

            if(ch == 9) result = TAB;

            if(!ch)
            {
               ch = getch();
               if(ch == 15) result = BACKTAB;
            };
            paint_but(25,18,"OK",passivebuttoncol,menucol);

            break;
         case 8:
            my_gotoxy(lastrow+1,lastline+1);
            paint_but(41,18,"Abort",activebuttoncol,menucol);
            ch = getch();
            if(ch == 13) result = 0;
            if(ch == 27) result = 0;
            if(ch == 9) result = TAB;
            if(!ch)
            {
               ch = getch();
               if(ch == 15) result = BACKTAB;
            };
            paint_but(41,18,"Abort",passivebuttoncol,menucol);

            break;

      };

      if(result==TAB) result = 1;
      if(result==BACKTAB)
      {
         sel--;
         if(!sel) sel = 8;
      };

      if(result==1)
      {
         sel++;
         if(sel>8) sel = 1;
      };

   } while((result!=2) && (result!=0));


   drestore(save);
   sec_free(save);
   my_gotoxy(lastrow+1,lastline+1);
   if(!result) return;

   cp = strrchr(fname,'\\');
   *cp = 0;
   strcpy(dir,fname);
   strcpy(fname,cp+1);
   make_path(dir);

   strcpy(s,fn_path);
   if(s[strlen(s)-1] == '\\') s[strlen(s)-1] = '\0';
   chdir(s);
  
   strcat(dir,"\\");
   strcpy(s,fname);

   generate_path(dir,s,".DES",fname);
   if((f = fopen(fname,"rt")) != NULL)
   {
      fclose(f);
      sprintf(fname,"%s already existing!",s);
      ok_box("Error",fname);
      return;
   };

   if((f = fopen(fname,"wt")) == NULL)
   {
      sprintf(fname,"Error while creating %s",s);
      ok_box("Error",fname);
      return;
   };


   fprintf(f,"TITLE %s\n",titel);
   fprintf(f,"DATE %s%s%s\n",tag,monat,jahr);
   fclose(f);

   kill_definitions();
   strcpy(title,titel);
   sprintf(date,"%s%s%s",tag,monat,jahr);
   strcpy(defname,fname);

   generate_path(fn_path,LAST_CONT_NAME,"",s);
   f = fopen(s,"wt");
   fputs(defname,f);
   fclose(f);
 
   bands = 0;
   valid = 0;
   firstdef = NULL;
   lastdef = NULL;

   new_band();

   if(!bands)
   {
      title[0] = 0;
      date[0] = 0;
      defname[0] = 0;
      kill_definitions();
   };

   return;
};

void new_band()
{
   void *save;
   unsigned char res;
   unsigned char sel;
   char titel[60];
   char gruppe[3];
   char band[9];
   char call[14];
   char loc[7];
   char ch;
   char hlp[80];
   char fname[80];
   struct definition *def;
   FILE *f;
   char *cp;

   char *bandname[12] =
   {"144 MHz","432 MHz","1.3 GHz","2.3 GHz","3.4 GHz","5.7 GHz","10 GHz",
    "24 GHz","47 GHz","76 GHz","144 GHz","241 GHz"};
   unsigned group;


   if(!defname[0]) return;

   strcpy(titel,title);
   gruppe[0] = 0;
   strcpy(call,def_call);
   strcpy(loc,def_loc);
   strupr(call);
   strupr(loc);
   band[0] = 0;

   save = make_win(10,4,70,16,menucol);

   dprint(31,5,menucol," Create new band");
   dprint(13,7,menucol,"Title");
   dfill(12,8,68,8,' ',menuinvcol);
   dprint(12,8,menuinvcol,titel);
   dprint(20,10,menucol,"Section");
   dfill(35,10,37,10,' ',menuinvcol);
   dprint(43,10,menucol,"Band");
   dfill(48,10,56,10,' ',menuinvcol);
   dprint(18,12,menucol,"Call");
   dfill(23,12,37,12,' ',menuinvcol);
   dprint(23,12,menuinvcol,call);
   dprint(43,12,menucol,"Locator");
   dfill(51,12,57,12,' ',menuinvcol);
   dprint(51,12,menuinvcol,loc);

   paint_but(25,14,"OK",passivebuttoncol,menucol);
   paint_but(41,14,"Abort",passivebuttoncol,menucol);

   sel = 1;
   do
   {
      switch(sel)
      {
         case 1:
            res = input_string(12,8,56,56,INSERT|DO_TAB,menuinvcol,titel);
            if(res==1) res = TAB;
            break;
         case 2:
            res = input_string(35,10,2,2,INSERT|DO_TAB,menuinvcol,gruppe);
            if(res)
            {
               if(gruppe[0])
               {
                  if((strlen(gruppe) == 1) && isdigit(gruppe[0]))
                  {
                     strcpy(hlp,"0");
                     strcat(hlp,gruppe);
                     strcpy(gruppe,hlp);
                     dprint(35,10,menuinvcol,gruppe);
                  };
                  if(!band[0])
                  {
                     group = atoi(gruppe);
                     if(group && (group < 25))
                     {
                        group--;
                        group>>=1;
                        strcpy(band,bandname[group]);
                        for(def=firstdef;def&&strcmp(def->band,band);def = def->next) ;
                        if(def) strcpy(band,"");
                        dprint(48,10,menuinvcol,band);
                     };
                  };
               };
            };

            if(res==1) res = TAB;
            break;

         case 3:
            res = input_string(48,10,8,8,INSERT|DO_TAB,menuinvcol,band);
            if(res)
            {
               if(band[0])
               {
                  if(firstdef)
                  {
                     for(def=firstdef;def&&strcmp(def->band,band);def = def->next) ;
                     if(def)
                     {
                        band[0] = 0;
                        dfill(48,10,56,10,' ',menuinvcol);
                        sound(100);
                        delay(600);
                        nosound();
                        res = 2;
                     };
                  };
               };
            };

            if(res==1) res = TAB;
            break;
         case 4:
            res = input_string(23,12,14,14,UPCASE|INSERT|CALL_CONVERT|DO_TAB,menuinvcol,call);
            if(res==1) res = TAB;
            break;
         case 5:
            res = input_string(51,12,6,6,UPCASE|INSERT|DO_TAB,menuinvcol,loc);
            if(!valid_locator(loc))
            {
               loc[0] = 0;
               dfill(42,12,48,12,' ',menuinvcol);
            };
            if(res==1) res = TAB;
            break;
         case 6:
            paint_but(25,14,"OK",activebuttoncol,menucol);
            ch = getch();

            if(ch == 13)
            {
               if(!gruppe[0])
               {
                  my_gotoxy(80,lastline+1);
                  ok_box("Warning","You must enter the section you want to participate in!");
                  res = TAB;
                  sel = 1;
               }
               else
               {
                  if(!band[0])
                  {
                     my_gotoxy(80,lastline+1);
                     ok_box("Warning","You must enter a symbolic name for the band!");
                     res = TAB;
                     sel = 2;
                  }
                  else res = 1;
               };
            };

            if(ch == 27) res = 0;

            if(ch == 9) res = TAB;

            if(!ch)
            {
               ch = getch();
               if(ch == 15) res = BACKTAB;
            };
            break;
         case 7:
            paint_but(41,14,"Abort",activebuttoncol,menucol);
            ch = getch();
            if(ch == 13) res = 0;
            if(ch == 27) res = 0;
            if(ch == 9) res = TAB;
            if(!ch)
            {
               ch = getch();
               if(ch == 15) res = BACKTAB;
            };
            break;
      };

      if(res==TAB)
      {
         sel++;
         if(sel>6) sel = 1;
      };
      if(res==BACKTAB)
      {
         sel--;
         if(!sel) sel = 6;
      };

   } while(res>1);

   drestore(save);
   sec_free(save);
   my_gotoxy(80,lastline+1);


   if(res)
   {
      fnsplit(defname,hlp,hlp,fname,hlp);
      strcpy(hlp,fname);
      strcat(hlp,gruppe);  
      hlp[8] = 0;
      generate_path(defname,hlp,".CNT",fname);
      save = make_win(10,6,70,9,menucol);

      dprint(12,7,menucol,"filename:");
      res = input_string(12,8,54,54,INSERT|UPCASE,menuinvcol,fname);
    
      drestore(save);
      sec_free(save);
      my_gotoxy(80,lastline+1);

      if(res)
      {
         if((f = fopen(fname,"rb")) != NULL)
         {
            fclose(f);
            sprintf(hlp,"%s already existing!",fname);
            ok_box("Error",hlp);
            return;
         };

         create_new_contest(fname,call,loc,titel);
         def = sec_alloc(DEF_SIZE);
         if(!firstdef) firstdef = def;
         strcpy(def->fname,fname);
         strcpy(def->band,band);
         def->prev = lastdef;
         if(lastdef) lastdef->next = def;
         lastdef = def;
         def->next = NULLdefinition;
         bands++;
         valid++;

         cp = strrchr(fname,'\\');
         if(cp) cp++;
         else cp = fname;
         f = fopen(defname,"at");
         fprintf(f,"%s = %s\n",band,cp);
         fclose(f);
      };
   };
  

   def = firstdef;
   chdir(fn_path);
   disp_cnt_data();
   my_gotoxy(80,lastline+1);
};


void do_load_contest()
{
   void *save;
   char result;
   char s[128];
   char *cp;
   FILE *f;
   struct definition *def;
   char dummy[128];
   char ext[5];

  
   strcpy(s,logdir);
   if(s[strlen(s)-1] != '\\') strcat(s,"\\");
   strcat(s,"*.DES");
   save = make_win(20,8,60,11,menucol);

   dprint(23,9,menucol,"filename of DES-file:");
   result = input_string(22,10,36,36,INSERT|UPCASE,menuinvcol,s);
  
   my_gotoxy(80,lastline+1);
   drestore(save);
   sec_free(save);

   if(result)
   {
      generate_path(logdir,s,".DES",s);
      cp = strchr(s,'?');
      if(!cp) cp = strchr(s,'*');
      if(cp)  
      { 
         result = file_select(s,menucol,menuinvcol);
         if(!result) return;
      };

      fnsplit(s,dummy,dummy,dummy,ext);
      if(strcmpi(ext,".DES"))
         if(!yes_no_box("Check","Extension is not .DES - load anyway?"))
           return;

      if((f = fopen(s,"rt")) == NULL)
      {
         sprintf(s,"%s not found!",s);
         ok_box("Error",s);
         return;
      };
      fclose(f);
    
      strcpy(defname,s);

      generate_path(fn_path,LAST_CONT_NAME,"",s);
      f = fopen(s,"wt");
      fputs(defname,f);
      fclose(f);
 
      kill_definitions();

      if(!load_definitions(defname,date,title))
      {
         defname[0] = 0;
         date[0] = 0;
         title[0] = 0;
         bands = 0;
         valid = 0;
      }
      else
      {
         valid = 0;
         if(firstdef)
         {
            for(def=firstdef;def;def=def->next)
            {
               def->found = (f = fopen(def->fname,"rb")) != NULL;
               if(def->found)
               {
                  valid++;
                  fclose(f);
               };
            };
         };
      };
  
      cntname[0] = 0;
      act_def = firstdef;
      disp_cnt_data();

      my_gotoxy(80,lastline+1);

      dfill(0,lastline,lastrow,lastline,' ',titlecol);
   };  
};


void do_stat()
{
   void *save;
   unsigned char selection;
   unsigned char ch;
   void *save2;
 
 
   if(!cntname[0]) return;

   load_prefixes();
   open_contest_file(cntname,act_def->band);
  
   save = make_win(50,7,72,18,menucol);

   dprint(52,8,menucol,"Countries");
   dprint(52,9,menucol,"Squares...");
   dprint(52,10,menucol,"QSO-maps...");
   dprint(52,11,menucol,"QSO-statistics...");
   dprint(52,12,menucol,"Distances");
   dprint(52,13,menucol,"Direction");
   dprint(52,14,menucol,"Create report...");

   dfill(51,15,71,15,196,(menucol&0xF0)+YELLOW);
   dprint(52,16,menucol,"Show contest-data");
   dprint(52,17,menucol,"Add contest-data");

   selection = 8;
   dcolor(51,8,71,8,menuinvcol);
  
   do
   {
      dfill(0,lastline,lastrow,lastline,' ',titlecol);
      dprint(1,lastline,titlecol,"ESC - back");
    
      do
      {
         ch = getch();
         if(ch==27) selection = 0;
         if(!ch)
         {
            ch = getch();
            switch(ch)
            {
               case 72:
                  dcolor(51,selection,71,selection,menucol);
                  selection--;
                  if(selection<8) selection = 17;
                  if(selection==15) selection--;
                  dcolor(51,selection,71,selection,menuinvcol);
                  break;
               case 80:
                  dcolor(51,selection,71,selection,menucol);
                  selection++;
                  if(selection>17) selection = 8;
                  if(selection==15) selection++;
                  dcolor(51,selection,71,selection,menuinvcol);
                  break;  
            };
         };
      } while((ch!=13)&&(ch!=27));
  
      switch(selection)
      {
         case 8:
            do_country_stat();
            break;
         case 9:
            save2 = make_win(38,9,55,13,menucol);
            dprint(40,10,menucol,"Map (text)");
            dprint(40,11,menucol,"Map (graphics)");
            dprint(40,12,menucol,"Best squares");

            selection = 10;
            do
            {
               dcolor(39,selection,54,selection,menuinvcol);

               ch = getch();
               if(!ch)
               {
                  ch = getch();
                  switch(ch)
                  {
                     case 80:
                     dcolor(39,selection,54,selection,menucol);
                     selection++;
                     if(selection>12) selection = 10;
                     break;
                     case 72:
                     dcolor(39,selection,54,selection,menucol);
                     selection--;
                     if(selection<10) selection = 12;
                     break;
                  };
               }
               else if(ch == 27) selection = 0;
            } while(ch != 13 && selection);

            drestore(save2);
            sec_free(save2);
            if(selection)
            {
               switch(selection)
               {
                  case 10:
                     do_square_map();
                     break;
                  case 11:
                     load_map_data();
                     show_square_graph();
                     free_map_data();
                     my_gotoxy(lastrow+1,lastline+1);
                     break;
                  case 12:
                     do_top_squares();
                     break;
               };
            };
            selection = 9;
            break;
         case 10:
            save2 = make_win(38,10,55,13,menucol);
            dprint(40,11,menucol,"All QSOs");
            dprint(40,12,menucol,"Only DX...");

            selection = 11;
            do
            {
               dcolor(39,selection,54,selection,menuinvcol);

               ch = getch();
               if(!ch)
               {
                  ch = getch();
                  switch(ch)
                  {
                     case 80:
                        dcolor(39,selection,54,selection,menucol);
                        selection++;
                        if(selection>12) selection = 11;
                        break;
                     case 72:
                        dcolor(39,selection,54,selection,menucol);
                        selection--;
                        if(selection<11) selection = 12;
                        break;
                  };
               }
               else if(ch == 27) selection = 0;
            } while(ch != 13 && selection);

            drestore(save2);
            sec_free(save2);
            if(selection)
            {
               switch(selection)
               {
                  case 11:
                     load_map_data();
                     do_all_map();
                     free_map_data();
                     my_gotoxy(lastrow+1,lastline+1);
                     break;
                  case 12:
                     load_map_data();
                     do_dx_map();
                     free_map_data();
                     my_gotoxy(lastrow+1,lastline+1);
                     break;
               };
            };
            selection = 10;
            break;
         case 11:
            do_time_stat(do_bar_stat);
            break;
         case 12:
            do_distances();
            break;
         case 13:
            do_dir_stat();
            break;
         case 14:
            do_report();
            break;
         case 16:
            disp_stn_data();
            break;
         case 17:
            add_stn_data();
            break;
      };
      
   } while(selection);
  
   kill_all_qso();
   kill_all_squares();
   kill_all_prefixes();

   drestore(save);
   sec_free(save);
   my_gotoxy(80,lastline+1);

   dfill(0,lastline,lastrow,lastline,' ',titlecol);
};



void print_header(FILE *f, long sum, uchar max_page)
{
   unsigned i;

   for(i=0;i<(80-strlen(h.desc))/2;i++) fprintf(f," ");
   fprintf(f,"%s\n",h.desc);
   fprintf(f,"FiveNine        Call: %-14s  Locator: %s",h.owncall,h.ownloc);
   fprintf(f,"           ");
   fprintf(f,"Page: %02d/%02d\n\n",pages,max_page);

   fprintf(f,"     %c",218);
   for(i=0;i<71;i++) fprintf(f,"\xC4");
   fprintf(f,"\xBF\n");
  
  
   fprintf(f,"     \xB3 Date   ");
   fprintf(f," Time ");
   fprintf(f,"  Call         ");
   fprintf(f," RST snd");
   fprintf(f," RST rcv");
   fprintf(f," Locator");
   fprintf(f," Points");
   fprintf(f,"  Comment  \xB3\n");

 
   fprintf(f,"     %c",195);
   for(i=0;i<8;i++) fprintf(f,"\xC4");
   fprintf(f,"\xC5");

   for(i=0;i<5;i++) fprintf(f,"\xC4");
   fprintf(f,"\xC5");

   for(i=0;i<14;i++) fprintf(f,"\xC4");
   fprintf(f,"\xC5");

   for(i=0;i<7;i++) fprintf(f,"\xC4");
   fprintf(f,"\xC5");

   for(i=0;i<7;i++) fprintf(f,"\xC4");
   fprintf(f,"\xC5");

   for(i=0;i<7;i++) fprintf(f,"\xC4");
   fprintf(f,"\xC5");

   for(i=0;i<6;i++) fprintf(f,"\xC4");
   fprintf(f,"\xC5");

   for(i=0;i<10;i++) fprintf(f,"\xC4");
   fprintf(f,"\xB4\n");

   if(sum)
   {
      for(i=0;i<39;i++) fprintf(f," ");
      fprintf(f,"sum carried forward: ");
      fprintf(f,"%6ld\n",sum);
   };
};


void print_intro(FILE *f,unsigned char NLQ)
{
   unsigned i;
   void *save;
   unsigned char item;
   unsigned char result;
   unsigned char ch;
   unsigned char s[256];
  
   unsigned char wettbewerb[21];
   unsigned char datum[12];
   unsigned char gruppe[4];
   unsigned char klasse[3];
   unsigned char DOK[5];
   unsigned char modus[3];
  
   unsigned char name[42];
   unsigned char strasse[42];
   unsigned char ort[42];
  
   unsigned char sender[22];
   unsigned char empfaenger[22];
   unsigned char antenne[22];
   unsigned char ueber_grund[5];
   unsigned char ueber_nn[6];
   unsigned char leistung[7];
   unsigned char eingangsstufe[22];
   
   unsigned char dataname[60];
   FILE *dataf;
   unsigned char *cp;
 
 
   strcpy(dataname,cntname);
   cp = strchr(dataname,'.');
   *cp = 0;
   strcat(dataname,".DAT");
  
   if((dataf = fopen(dataname,"rt"))!=NULL)
   {
      fgets(wettbewerb,21,dataf);
      if(wettbewerb[strlen(wettbewerb)-1] == '\n') wettbewerb[strlen(wettbewerb)-1] = 0;
      fgets(datum,12,dataf);
      if(datum[strlen(datum)-1] == '\n') datum[strlen(datum)-1] = 0;
      fgets(gruppe,4,dataf);  
      if(gruppe[strlen(gruppe)-1] == '\n') gruppe[strlen(gruppe)-1] = 0;
      fgets(klasse,3,dataf);
      if(klasse[strlen(klasse)-1] == '\n') klasse[strlen(klasse)-1] = 0;
      fgets(DOK,7,dataf);
      if(DOK[strlen(DOK)-1] == '\n') DOK[strlen(DOK)-1] = 0;
      fgets(modus,3,dataf);
      if(modus[strlen(modus)-1] == '\n') modus[strlen(modus)-1] = 0;
      fgets(name,42,dataf);
      if(name[strlen(name)-1] == '\n') name[strlen(name)-1] = 0;
      fgets(strasse,42,dataf);
      if(strasse[strlen(strasse)-1] == '\n') strasse[strlen(strasse)-1] = 0;
      fgets(ort,42,dataf);
      if(ort[strlen(ort)-1] == '\n') ort[strlen(ort)-1] = 0;
      fgets(sender,22,dataf);
      if(sender[strlen(sender)-1] == '\n') sender[strlen(sender)-1] = 0;
      fgets(empfaenger,22,dataf);
      if(empfaenger[strlen(empfaenger)-1] == '\n') empfaenger[strlen(empfaenger)-1] = 0;
      fgets(antenne,22,dataf);
      if(antenne[strlen(antenne)-1] == '\n') antenne[strlen(antenne)-1] = 0;
      fgets(ueber_grund,5,dataf);
      if(ueber_grund[strlen(ueber_grund)-1] == '\n') ueber_grund[strlen(ueber_grund)-1] = 0;
      fgets(ueber_nn,6,dataf);
      if(ueber_nn[strlen(ueber_nn)-1] == '\n') ueber_nn[strlen(ueber_nn)-1] = 0;
      fgets(leistung,7,dataf);
      if(leistung[strlen(leistung)-1] == '\n') leistung[strlen(leistung)-1] = 0;
      fgets(eingangsstufe,22,dataf);
      if(eingangsstufe[strlen(eingangsstufe)-1] == '\n') eingangsstufe[strlen(eingangsstufe)-1] = 0;
      fclose(dataf);
      item = 17;
   }
   else
   {
      wettbewerb[0] = 0;
      datum[0] = 0;
      gruppe[0] = 0;
      klasse[0] = 0;
      DOK[0] = 0;
      modus[0] = 0;
      name[0] = 0;
      strasse[0] = 0;
      ort[0] = 0;
      sender[0] = 0;
      empfaenger[0] = 0;
      eingangsstufe[0] = 0;
      antenne[0] = 0;
      leistung[0] = 0;
      ueber_grund[0] = 0;
      ueber_nn[0] = 0;
      item = 1;
   };
  
   save = dsave(0,0,lastrow,lastline);
  
   dfill(0,0,lastrow,lastline,' ',menucol);
  
   dprint(3,0,menucol,"Data for summary sheet");
   dfill(3,1,22,1,196,menucol);

   dprint(1,3,menucol,"Competition");
   dfill(13,3,32,3,' ',menuinvcol);
   dprint(13,3,menuinvcol,wettbewerb);
  
   dprint(36,3,menucol,"Date");
   dfill(42,3,52,3,' ',menuinvcol);
   dprint(42,3,menuinvcol,datum);
  
   dprint(56,3,menucol,"Section");
   dfill(71,3,73,3,' ',menuinvcol);
   dprint(71,3,menuinvcol,gruppe);

   dprint(1,5,menucol,"licence class");
   dfill(15,5,16,5,' ',menuinvcol);
   dprint(15,5,menuinvcol,klasse);

   dprint(21,5,menucol,"Club");
   dfill(26,5,31,5,' ',menuinvcol);
   dprint(26,5,menuinvcol,DOK);

   dprint(36,5,menucol,"S)ingle/M)ulti");
   dfill(53,5,55,5,' ',menuinvcol);
   dprint(53,5,menuinvcol,modus);

   dfill(0,6,79,6,196,menucol);
  
   dprint(1,8,menucol,"Name of responsible amateur");
   dfill(1,9,41,9,' ',menuinvcol);
   dprint(1,9,menuinvcol,name);

   dprint(1,11,menucol,"Street and no.");
   dfill(1,12,41,12,' ',menuinvcol);
   dprint(1,12,menuinvcol,strasse);

   dprint(1,14,menucol,"Postcode and City");
   dfill(1,15,41,15,' ',menuinvcol);
   dprint(1,15,menuinvcol,ort);

   dfill(0,16,79,16,196,menucol);
   
   dprint(1,18,menucol,"Sender");
   dfill(15,18,35,18,' ',menuinvcol);
   dprint(15,18,menuinvcol,sender);

   dprint(41,18,menucol,"Receiver");
   dfill(51,18,71,18,' ',menuinvcol);
   dprint(51,18,menuinvcol,empfaenger);

   dprint(1,20,menucol,"Preamplifier");
   dfill(15,20,35,20,' ',menuinvcol);
   dprint(15,20,menuinvcol,eingangsstufe);

   dprint(41,20,menucol,"Antenna");
   dfill(51,20,71,20,' ',menuinvcol);
   dprint(51,20,menuinvcol,antenne);

   dprint(1,22,menucol,"Used power");
   dfill(18,22,24,22,' ',menuinvcol);
   dprint(18,22,menuinvcol,leistung);
   dprint(25,22,menucol,"W");
  
   dprint(29,22,menucol,"Ant. over ground");
   dfill(48,22,51,22,' ',menuinvcol);
   dprint(48,22,menuinvcol,ueber_grund);
   dprint(52,22,menucol,"m");
  
   dprint(56,22,menucol,"Antenna ASL");
   dfill(72,22,76,22,' ',menuinvcol);
   dprint(72,22,menuinvcol,ueber_nn);
   dprint(77,22,menucol,"m");
  
  
   dprint(50,6,menucol,"\xC2");
   dprint(50,16,menucol,"\xC1");
   for(ch=7;ch<16;ch++) dprint(50,ch,menucol,"\xB3");
  
   paint_but(60,9,"Print",passivebuttoncol,menucol);
   paint_but(60,12,"Abort",passivebuttoncol,menucol);
  
   do
   {
      switch(item)
      {
         case 1:
            result = input_string(13,3,19,19,DO_TAB,menuinvcol,wettbewerb);
            break;
         case 2:
            result = input_string(42,3,10,10,DO_TAB,menuinvcol,datum);
            break;
         case 3:
            result = input_string(71,3,2,2,DO_TAB|DIGITS_ONLY,menuinvcol,gruppe);
            break;
         case 4:
            result = input_string(15,5,1,1,DO_TAB|UPCASE,menuinvcol,klasse);
            break;
         case 5:
            result = input_string(26,5,5,5,DO_TAB|UPCASE,menuinvcol,DOK);
            break;
         case 6:
            result = input_string(53,5,1,1,DO_TAB|UPCASE,menuinvcol,modus);
            break;
         case 7:
            result = input_string(1,9,40,40,DO_TAB,menuinvcol,name);
            break;
         case 8:
            result = input_string(1,12,40,40,DO_TAB,menuinvcol,strasse);
            break;
         case 9:
            result = input_string(1,15,40,40,DO_TAB,menuinvcol,ort);
            break;    
         case 10:
            result = input_string(15,18,20,20,DO_TAB,menuinvcol,sender);
            break;
         case 11:
            result = input_string(51,18,20,20,DO_TAB,menuinvcol,empfaenger);
            break;
         case 12:
            result = input_string(15,20,20,20,DO_TAB,menuinvcol,eingangsstufe);
            break;
         case 13:
            result = input_string(51,20,20,20,DO_TAB,menuinvcol,antenne);
            break;      
         case 14:
            result = input_string(18,22,5,5,DO_TAB,menuinvcol,leistung);
            break;
         case 15:
            result = input_string(48,22,3,3,DO_TAB|DIGITS_ONLY,menuinvcol,ueber_grund);
            break;
         case 16:
            result = input_string(72,22,4,4,DO_TAB|DIGITS_ONLY,menuinvcol,ueber_nn);
            break;    
         case 17:
            my_gotoxy(80,lastline+1);
            paint_but(60,9,"Print",activebuttoncol,menucol);
            ch = getch();
            if(ch == 13) result = 90;
            if(ch == 27) result = 0;
            if(ch == 9)
            {
               result = TAB;
               paint_but(60,9,"Print",passivebuttoncol,menucol);
            }; 
            if(!ch)
            {
               ch = getch();
               if(ch == 15)
               {
                  result = BACKTAB;
                  paint_but(60,9,"Print",passivebuttoncol,menucol);
               };
            };      
            break;
         case 18:
            my_gotoxy(80,lastline+1);
            paint_but(60,12,"Abort",activebuttoncol,menucol);
            ch = getch();
            if(ch == 13) result = 90;
            if(ch == 27) result = 0;
            if(ch == 9)
            {
               result = TAB;
               paint_but(60,12,"Abort",passivebuttoncol,menucol);
            };
            if(!ch)
            {
               ch = getch();
               if(ch == 15)
               {
                  result = BACKTAB;
                  paint_but(60,12,"Abort",passivebuttoncol,menucol);
               };
            };      
            break;
      }; 
    
      if(result == 1) result = TAB;
      if(result == TAB)
      {
         item++;
         if(item > 18) item = 1;
      };
      if(result == BACKTAB)
      {
         item--;
         if(item < 1) item = 18;
      };
   } while((result!=0)&&(result!=90));                
        
   drestore(save);
   sec_free(save);
   my_gotoxy(80,lastline+1);
  
   if(!result) return;
  
   dataf = fopen(dataname,"wt");
   fprintf(dataf,"%s\n",wettbewerb);
   fprintf(dataf,"%s\n",datum);
   fprintf(dataf,"%s\n",gruppe);
   fprintf(dataf,"%s\n",klasse);
   fprintf(dataf,"%s\n",DOK);
   fprintf(dataf,"%s\n",modus);
   fprintf(dataf,"%s\n",name);
   fprintf(dataf,"%s\n",strasse);
   fprintf(dataf,"%s\n",ort);
   fprintf(dataf,"%s\n",sender);
   fprintf(dataf,"%s\n",empfaenger);
   fprintf(dataf,"%s\n",antenne);
   fprintf(dataf,"%s\n",ueber_grund);
   fprintf(dataf,"%s\n",ueber_nn);
   fprintf(dataf,"%s\n",leistung);
   fprintf(dataf,"%s\n",eingangsstufe);
   fclose(dataf);
  
   if(NLQ)
   {
      if(printer_type == EPSON)
      {
         fprintf(f,EPS_DASHED_ZERO);
         fprintf(f,EPS_BIT8_ON);
      }
      else
      {
         fprintf(f,HP_PORTRAIT);
         fprintf(f,HP_MONOSPACED);
         fprintf(f,HP_FONT,"4099");
      };
   };

   switch(NLQ)
   {
      case 0:
         break;
      case 1:
         if(printer_type == EPSON) fprintf(f,EPS_NLQ_ON);
            else fprintf(f,HP_NLQ_ON);
         break;
      case 2:
         if(printer_type == EPSON) fprintf(f,EPS_DRAFT_ON);
            else fprintf(f,HP_DRAFT_ON);
         break;
   };

   s[20] = 0;
   memset(s,196,20);
  
   fprintf(f,"%c%s%c   %c%c%c%c   %c%c%c%c\n",218,s,191,218,196,196,191,218,196,196,191);
   fprintf(f,"%c                    %c   %c  %c   %c  %c\n",179,179,179,179,179,179);
   fprintf(f,"%c%s%c   %c%c%c%c   %c%c%c%c\n\n",192,s,217,192,196,196,217,192,196,196,217);
  
   memset(s,' ',((80-strlen(h.desc))/2)-8);
   s[((80-strlen(h.desc))/2)-8] = 0;
   fprintf(f,"FiveNine%s%s\n",s,h.desc);
   fprintf(f,"%s",s);
   memset(s,'_',strlen(h.desc));
   s[strlen(h.desc)] = 0;
   fprintf(f,"        %s\n\n\n",s);


   if(!wettbewerb[0]) strcpy(wettbewerb,"____________________");
   if(!datum[0]) strcpy(datum,"__________");
   if(!gruppe[0]) strcpy(gruppe,"__");
   if(!klasse[0]) strcpy(klasse,"_");
   if(!DOK[0]) strcpy(DOK,"_____");
   if(!modus[0]) strcpy(modus,"_");
   if(!name[0]) {memset(name,'_',40);name[40] = 0;};
   if(!strasse[0]) {memset(strasse,'_',40);strasse[40] = 0;};
   if(!ort[0]) {memset(ort,'_',40);ort[40] = 0;};
   if(!sender[0]) strcpy(sender,"____________________");
   if(!empfaenger[0]) strcpy(empfaenger,"____________________");
   if(!antenne[0]) strcpy(antenne,"____________________");
   if(!eingangsstufe[0]) strcpy(eingangsstufe,"____________________");
   if(!leistung[0]) strcpy(leistung,"_____");
   if(!ueber_grund[0]) strcpy(ueber_grund,"___");
   if(!ueber_nn[0]) strcpy(ueber_nn,"____");
   
   fprintf(f,"     Competition:%23s",wettbewerb);
   fprintf(f,"     Date :%28s\n\n\n",datum);
   fprintf(f,"     Section       :%19s",gruppe);
   fprintf(f,"     Call      :%23s\n\n\n",h.owncall);
   fprintf(f,"     Licence class:%20s",klasse);
   fprintf(f,"     Club:%29s\n\n\n",DOK);
   fprintf(f,"     Worked squares        :%11d",h.squares);
   fprintf(f,"     Locator:%26s\n\n\n",h.ownloc);
   fprintf(f,"     Worked countries  :%15d",h.countries);
   fprintf(f,"     Single/Multi (S/M)     :%11s\n\n\n",modus);
   fprintf(f,"     Points per QSO:%19d",h.average);
   fprintf(f,"     Valid contacts      :%13d\n\n\n",h.qso_cnt-h.invalid);
   fprintf(f,"     Longest QRB        :%11d km",h.ODX);
   fprintf(f,"     Claimed score  :%18ld\n\n\n",h.points);
  
   memset(s,'_',80);
   s[80] = 0;
   fprintf(f,"%s\n\n",s);
  
   fprintf(f,"   Address of the responsible amateur for this operation:\n\n");
   fprintf(f,"   %s\n\n",name);
   fprintf(f,"   %s\n\n",strasse);
   fprintf(f,"   %s\n\n",ort);
  
   fprintf(f,"%s\n\n",s);
  
   fprintf(f,"   Used equipment:\n\n");
   fprintf(f,"   Sender:   %21s",sender);
   fprintf(f,"          Output power    :%6s W\n\n",leistung);
   fprintf(f,"   Receiver :%21s",empfaenger);
   fprintf(f,"          Preamplifier : %-20s\n\n",eingangsstufe);
   fprintf(f,"   Antenna:   %-20s",antenne);
   fprintf(f,"    Height over gr.:%4s m",ueber_grund);
   fprintf(f,"   ASL    :%5s m\n\n",ueber_nn);

   fprintf(f,"   I confirm that the operation followed the regulations of the\n\n");
   fprintf(f,"   competition and the licence.\n\n");
  
   strcpy(s,"____________    ________________________");
   fprintf(f,"   Responsible amateur         :    %s\n\n",s);
   fprintf(f,"   Other anateurs              :    %s\n\n",s);
   fprintf(f,"   Further amateurs on              %s\n\n",s);
   fprintf(f,"   on the back.                     %s\n\n",s);
   fprintf(f,"                                       Call          Signature\n");
  
   fprintf(f,FORMFEED);
};


void print_log(FILE *f,unsigned char NLQ)
{
   unsigned lines = 1;
   unsigned cnt = 0;
   struct qso *q;
   unsigned i;
   unsigned long sum;
   uchar max_page;
   uchar qso_date[12];
   uchar old_date[12];
   uchar hlp;
   uchar s[4];
   uchar old_hour;
  
   pages = 1;
   old_date[0] = 0;  
   old_hour = 25;

   if(NLQ)
   {
      if(printer_type == EPSON)
      {
         fprintf(f,EPS_DASHED_ZERO);
         fprintf(f,EPS_BIT8_ON);
      }
      else
      {
         fprintf(f,HP_PORTRAIT);
         fprintf(f,HP_MONOSPACED);
         fprintf(f,HP_FONT,"4099");
      };
   };

   switch(NLQ)
   {
      case 0:
         break;
      case 1:
         if(printer_type == EPSON) fprintf(f,EPS_NLQ_ON);
            else fprintf(f,HP_NLQ_ON);
         break;
      case 2:
         if(printer_type == EPSON) fprintf(f,EPS_DRAFT_ON);
            else fprintf(f,HP_DRAFT_ON);
         break;
   };

   max_page = 1;
   sum = 56;
   while(sum<h.qso_cnt)
   {
      max_page++;
      sum+=55;
   };
   sum = 0;

   print_header(f,sum,max_page);
   q = firstqso;

   while(q)
   {
      if(always_date) old_date[0] = 0;
      if(q->hour<14)
      {
         strncpy(qso_date,date,2);
         qso_date[2] = 0;
         hlp = atoi(qso_date);
         sprintf(qso_date,"%02d",hlp+1);
      }
      else strncpy(qso_date,date,2);
      qso_date[2] = 0;
      strcat(qso_date,".");
      strncpy(s,date+2,2);
      s[2] = 0;
      strcat(qso_date,s);
      strcat(qso_date,".");
      strcat(qso_date,date+4);

      if(!strcmp(qso_date,old_date)) strcpy(qso_date,"        ");
      else strcpy(old_date,qso_date);

      fprintf(f,"      %s",qso_date);

      if(always_hour) old_hour = 25;
      if(q->hour==old_hour) strcpy(s,"  ");
      else sprintf(s,"%02d",q->hour);
      old_hour = q->hour;      

      fprintf(f," %02s:%02d",s,q->min);
      if(strlen(q->call)<14) fprintf(f,"  %-13s",q->call);
         else fprintf(f," %-14s",q->call);

      if(q->nr > 999) fprintf(f," %3s%4d",q->rst_TX,q->nr);
         else fprintf(f," %3s %03d",q->rst_TX,q->nr);
      fprintf(f," %7s",q->rst_RX);
      fprintf(f,"  %6s",q->locator);
      if(q->points) fprintf(f," %6d",q->points);
         else fprintf(f,"    ***");
      if(q->points)
      {
         if(q->points==h.ODX) fprintf(f,"       ODX ");
         else fprintf(f,"           ");
      }
      else fprintf(f,"      DUPE ");

      fprintf(f,"\n");
      cnt++;
      sum+=q->points;

      if((lines == 56) && (q->next))
      {
         fprintf(f,"     \xC0");
         for(i=0;i<8;i++) fprintf(f,"\xC4");
         fprintf(f,"\xC1");

         for(i=0;i<5;i++) fprintf(f,"\xC4");
         fprintf(f,"\xC1");
 
         for(i=0;i<14;i++) fprintf(f,"\xC4");
         fprintf(f,"\xC1");

         for(i=0;i<7;i++) fprintf(f,"\xC4");
         fprintf(f,"\xC1");

         for(i=0;i<7;i++) fprintf(f,"\xC4");
         fprintf(f,"\xC1");

         for(i=0;i<7;i++) fprintf(f,"\xC4");
         fprintf(f,"\xC1");

         for(i=0;i<6;i++) fprintf(f,"\xC4");
         fprintf(f,"\xC1");

         for(i=0;i<10;i++) fprintf(f,"\xC4");
         fprintf(f,"\xD9\n");


         for(i=0;i<40;i++) fprintf(f," ");
         fprintf(f,"imtermediate score: %6ld\n\n",sum);
         fprintf(f,FORMFEED);
         pages++;

         old_date[0] = 0;
         old_hour = 25;

         
         print_header(f,sum,max_page);
         lines=1;
      };  
      lines++;
      q = q->next;
   };  

   fprintf(f,"     \xC0");
   for(i=0;i<8;i++) fprintf(f,"\xC4");
   fprintf(f,"\xC1");

   for(i=0;i<5;i++) fprintf(f,"\xC4");
   fprintf(f,"\xC1");

   for(i=0;i<14;i++) fprintf(f,"\xC4");
   fprintf(f,"\xC1");

   for(i=0;i<7;i++) fprintf(f,"\xC4");
   fprintf(f,"\xC1");

   for(i=0;i<7;i++) fprintf(f,"\xC4");
   fprintf(f,"\xC1");

   for(i=0;i<7;i++) fprintf(f,"\xC4");
   fprintf(f,"\xC1");

   for(i=0;i<6;i++) fprintf(f,"\xC4");
   fprintf(f,"\xC1");

   for(i=0;i<10;i++) fprintf(f,"\xC4");
   fprintf(f,"\xD9\n");


   for(i=0;i<47;i++) fprintf(f," ");
   fprintf(f,"final score: %6ld\n",sum);

   fprintf(f,FORMFEED);
};


void do_logprint()
{
   unsigned char selection;
   unsigned char ch;
   unsigned char result;
   void *save3;
   void *save2;
   void *save;
   char s[40];
   FILE *logf;
   unsigned char type;

   if(!cntname[0]) return;

   load_prefixes();
   open_contest_file(cntname,act_def->band);

   save = make_win(50,7,76,11,menucol);
  
   dprint(52,8,menucol,"Print summary sheet");
   dprint(52,9,menucol,"Print log");
   dprint(52,10,menucol,"Print both");
   
   selection = 8;
   dcolor(51,8,75,8,menuinvcol);
   do
   {
      dfill(0,lastline,lastrow,lastline,' ',titlecol);
      dprint(1,lastline,titlecol,"ESC - back");

      do
      {
         ch = getch();
         if(ch==27) selection = 0;
         if(!ch)
         {
            ch = getch();
            switch(ch)
            {
               case 72:
                  dcolor(51,selection,75,selection,menucol);
                  selection--;
                  if(selection<8) selection = 10;
                  dcolor(51,selection,75,selection,menuinvcol);
                  break;
               case 80:
                  dcolor(51,selection,75,selection,menucol);
                  selection++;
                  if(selection>10) selection = 8;
                  dcolor(51,selection,75,selection,menuinvcol);
                  break;
            };
         };  
      } while((ch!=13)&&(ch!=27));
  
      if(selection)
      {
         save3 = make_win(50,7,76,11,menucol);
         dprint(51,8,menucol,"ASCII only");
         dprint(51,9,menucol,"NLQ");
         dprint(51,10,menucol,"Draft");
      
         type = 8;
         dcolor(51,8,75,8,menuinvcol);
         do
         {
            ch = getch();
            if(ch==27) type = 0;
            if(!ch)
            {
               ch = getch();
               switch(ch)
               {
                  case 72:
                     dcolor(51,type,75,type,menucol);
                     type--;
                     if(type<8) type = 10;
                     dcolor(51,type,75,type,menuinvcol);
                     break;
                  case 80:
                     dcolor(51,type,75,type,menucol);
                     type++;
                     if(type>10) type = 8;
                     dcolor(51,type,75,type,menuinvcol);
                     break;
               };
            };
         } while((ch!=13)&&(ch!=27));
  
         if(type)
         {
            type-=8;
      
            save2 = make_win(45,3,75,5,menucol);
            dprint(47,3,menucol," filename ");
      
            strcpy(s,"PRN");
            result = input_string(46,4,28,28,UPCASE,menuinvcol,s);
      
            drestore(save2);
            sec_free(save2);
            my_gotoxy(80,lastline+1);
      
            if(result)
            {
               if(strcmpi(s,"PRN")&&strcmpi(s,"LPT1")&&strcmpi(s,"LPT2"))
               generate_path(cntname,s,type==8?".ASC":".PRN",s);

               if((logf = fopen(s,"w")) == NULL) ok_box("Error","File cannot be opened!");
               else
               {  
                  switch(selection)
                  {
                     case 8:
                        print_intro(logf,type);
                        break;
                     case 9:
                        print_log(logf,type);
                        break;
                     case 10:
                        print_intro(logf,type);
                        print_log(logf,type);
                        break;
                  };
                  fclose(logf);
               };
            };
         };

         drestore(save3);
         sec_free(save3);
      };
            
   } while(selection);

   drestore(save);
   sec_free(save);
   my_gotoxy(80,lastline+1);

   kill_all_qso();
   kill_all_squares();
   kill_all_prefixes();

   dfill(0,lastline,lastrow,lastline,' ',titlecol);
};


void do_stations()
{
   unsigned char ch;
   char sel;
   void *save;
   void *save2;
   char fname[30];
   unsigned result;


   save = make_win(40,7,62,12,menucol);
  
   dprint(42,8,menucol,"Browse...");
   dprint(42,9,menucol,"Add contest");
   dprint(42,10,menucol,"Purge...");
   dprint(42,11,menucol,"Reorganize");
  
   dcolor(41,8,61,8,menuinvcol);
  
   sel = 8;
  
   do
   {
      dfill(0,lastline,79,lastline,' ',titlecol);
      dprint(1,lastline,titlecol,"ESC - back");

      do
      {
         ch = getch();
         if(ch == 27)
         {
            ch = 13;
            sel = 0;
         };
         if(!ch)
         {
            ch = getch();
            switch(ch)
            {
               case 72:
                  dcolor(41,sel,61,sel,menucol);
                  sel--;
                  if(sel<8) sel = 11;
                  dcolor(41,sel,61,sel,menuinvcol);
                  break;
               case 80:
                  dcolor(41,sel,61,sel,menucol);
                  sel++;
                  if(sel>11) sel = 8;
                  dcolor(41,sel,61,sel,menuinvcol);
                  break;
            };
         };
      } while(ch!=13);
    
      switch(sel)
      {
         case 8:
            do_browse_stations();
            break;
         case 9:
            if(valid>1) act_def = select_band();
            if(act_def)
            {
               enable_band(act_def, 0);
               do_add_cnt();
               reorganisation(0);
            };
            break;
         case 10:
            purge_date();
            break;
         case 11:
            reorganisation(0);
            break;
      };
    
   } while(sel);
  
   drestore(save);
   sec_free(save);
   my_gotoxy(80,lastline+1);                      
};

void do_qsl_print()
{
   void *save;
   char sel = 6;
   char ch;
   struct pparams pp1;
   struct pparams pp2;
   char ende = 0;

   save = make_win(50,5,73,11,menucol);

   dprint(52,6,menucol,"Print-parameters...");
   dprint(52,7,menucol,"Test printing");
   dprint(52,8,menucol,"Print \"PSE QSL\"...");
   dprint(52,9,menucol,"Print \"TNX QSL\"...");
   dprint(52,10,menucol,"Back...");

   dcolor(51,sel,72,sel,menuinvcol);

   if(!get_print_params(&pp1))
   {
      set_def_params(&pp1);
      save_print_params(&pp1);
   };

   do
   {
      do
      {
         ch = getch();
         if(!ch)
         {
            ch = getch();
            switch(ch)
            {
               case 72:
                  dcolor(51,sel,72,sel,menucol);
                  sel--;
                  if(sel<6) sel = 10;
                  dcolor(51,sel,72,sel,menuinvcol);
                  break;
               case 80:
                  dcolor(51,sel,72,sel,menucol);
                  sel++;
                  if(sel>10) sel = 6;
                  dcolor(51,sel,72,sel,menuinvcol);
                  break;
            };
         };
      } while(ch!=13 && ch!=27);

      if(ch == 27) ende = 1;

      if(ch == 13)
      {
         switch(sel)
         {
            case 6:
               print_params(&pp1);
               save_print_params(&pp1);
               break;
            case 7:
               convert_print_params(&pp1,&pp2);
               test_print(&pp2);
               break;
            case 8:
               if(valid)
               {
                  if(valid>1) act_def = select_band();
                  if(act_def)
                  {
                     enable_band(act_def,0);
                     convert_print_params(&pp1,&pp2);
                     print_qsl(&pp2);
                  };
               };
               break;
            case 9:
               if(valid)
               {
                  if(valid>1) act_def = select_band();
                  if(act_def)
                  {
                     enable_band(act_def, 0);
                     convert_print_params(&pp1,&pp2);
                     print_tnx_qsl(&pp2);
                  };
               };
               break;
            case 10:
               ende = 1;
               break;
         };
      };
   } while(!ende);

   drestore(save);
   sec_free(save);
};


void make_csv_header(FILE *f, unsigned char delim)
{
   char tab[9][20] = {"Nummer","Std","Min","Call","RST geg","RST erh","Locator","Punkte","Richtung"};
   char tabs[9] = {0,8,12,16,32,42,51,60,68};

   unsigned i;
   char s[128];
   char hlp[2];
  
   s[0] = 0;
   for(i=0;i<9;i++)
   {
      strcat(s,tab[i]);
      switch(delim)
      {
         case 'T':
            if(i<8) strcat(s,"\x09");
            break;
         case 'S':
            if(i<8) strcat(s," ");
            break;
         case 'F':
            if(i<8) while(strlen(s)<tabs[i+1]) strcat(s," ");
            break;
         default:
            if(i<8) 
            {  
               hlp[0] = delim;
               hlp[1] = 0;
               strcat(s,hlp);
            };
            break;
      };
   };
   fprintf(f,"%s\n",s);
};


void make_delim(char *s, unsigned char delim, unsigned char i)
{
   char tabs[9] = {0,8,12,16,32,42,51,60,68};
   char hlp[2];
  
   switch(delim)
   {
      case 'T':
         strcat(s,"\x09");
         break;
      case 'S':
         strcat(s," ");
         break;
      case 'F':
         while(strlen(s)<tabs[i+1]) strcat(s," ");
         break;
      default:
         hlp[0] = delim;
         hlp[1] = 0;
         strcat(s,hlp);
         break;
   };
};


void write_sdf_line(FILE *f,unsigned char delim, struct qso q)
{
   char s[128];
   char hlp[20];
   char hlp2[20];
  
   s[0] = 0;
  
   sprintf(hlp,"%4d",q.nr);
   strcat(s,hlp);
   make_delim(s,delim,0);

   sprintf(hlp,"%02d:%02d",q.hour,q.min);
   strcat(s,hlp);
   make_delim(s,delim,2);
  
   sprintf(hlp,"%-14s",q.call);
   strcat(s,hlp);
   make_delim(s,delim,3);
  
   if(q.nr<1000) sprintf(hlp,"%s%03d",q.rst_TX,q.nr);
   else sprintf(hlp,"%s%4d",q.rst_TX,q.nr);
   sprintf(hlp2,"%7s",hlp);
   strcat(s,hlp2);
   make_delim(s,delim,4);
  
   sprintf(hlp,"%7s",q.rst_RX);
   strcat(s,hlp);
   make_delim(s,delim,5);
  
   sprintf(hlp,"%s",q.locator);
   strcat(s,hlp);
   make_delim(s,delim,6);
  
   sprintf(hlp,"%4d",q.points);
   strcat(s,hlp);
   make_delim(s,delim,7);
  
   sprintf(hlp,"%3d",q.direction);
   strcat(s,hlp);
   make_delim(s,delim,8);

   fprintf(f,"%s\n",s);
};


void write_csv_line(FILE *f,unsigned char delim, struct qso q)
{
   char s[128];
   char hlp[20];
  
   s[0] = 0;
  
   sprintf(hlp,"%d",q.nr);
   strcat(s,hlp);
   make_delim(s,delim,0);

   sprintf(hlp,"%02d",q.hour);
   strcat(s,hlp);
   make_delim(s,delim,1);
  
   sprintf(hlp,"%02d",q.min);
   strcat(s,hlp);
   make_delim(s,delim,2);
  
   sprintf(hlp,"%s",q.call);
   strcat(s,hlp);
   make_delim(s,delim,3);
  
   if(q.nr<1000) sprintf(hlp,"%s%03d",q.rst_TX,q.nr);
   else sprintf(hlp,"%s%4d",q.rst_TX,q.nr);
   strcat(s,hlp);
   make_delim(s,delim,4);
  
   sprintf(hlp,"%s",q.rst_RX);
   strcat(s,hlp);
   make_delim(s,delim,5);
  
   sprintf(hlp,"%s",q.locator);
   strcat(s,hlp);
   make_delim(s,delim,6);
  
   sprintf(hlp,"%d",q.points);
   strcat(s,hlp);
   make_delim(s,delim,7);
  
   sprintf(hlp,"%d",q.direction);
   strcat(s,hlp);
   make_delim(s,delim,8);

   fprintf(f,"%s\n",s);
};


unsigned char query_delim(char def)
{
   void *save;
   char sel;
   char ch;
   char res;
   char s[3];

   save = make_win(50,7,78,13,menucol);

   dprint(52,7,menucol," Delimiter ");
   dprintf(52,8,menucol,"Default (%c)",def);
   dprint(52,9,menucol,"ASCII 09 (Tabulator)");
   dprint(52,10,menucol,"1 Space");
   dprint(52,11,menucol,"Colums with spaces");
   dprint(52,12,menucol,"Other character...");

   sel = 8;
   dcolor(51,8,77,8,menuinvcol);

   do
   {
      ch = getch();
      if(!ch)
      {
         ch = getch();
         switch(ch)
         {
            case 72:
               dcolor(51,sel,77,sel,menucol);
               sel--;
               if(sel<8) sel = 12;
               dcolor(51,sel,77,sel,menuinvcol);
               break;
            case 80:
               dcolor(51,sel,77,sel,menucol);
               sel++;
               if(sel>12) sel = 8;
               dcolor(51,sel,77,sel,menuinvcol);
               break;
         };
      };
   } while(ch!=13 && ch!=27);

   drestore(save);
   sec_free(save);

   if(ch == 27) return(27);

   switch(sel)
   {
      case 8: return(def);
      case 9: return('T');
      case 10: return('S');
      case 11: return('F');
      case 12:
         save = make_win(50,14,76,17,menucol);

         dprint(52,15,menucol,"Enter delimiter:");
         s[0] = 0;
         res = input_string(62,16,1,1,INSERT,menuinvcol,s);
 
         drestore(save);
         sec_free(save);
         my_gotoxy(80,lastline+1);

         if(!res || s[0] == 0) return(27);
         return(s[0]);
   };
   return(27);
};


void export_sdf(char *fname)
{
   FILE *dstf;
   char delimiter;
   char s[80];
   struct qso *q;

   if((dstf = fopen(fname,"wt")) == NULL)
   {
      sprintf(s,"File %s cannot be opened!",fname);
      ok_box("Error",s);
      return;
   };

   if(!cntname[0]) return;

   load_prefixes();
   open_contest_file(cntname,act_def->band);

   delimiter = query_delim(':');
   if(delimiter == 27)
   {
      fclose(dstf);
      return;
   };

   q = firstqso;
   while(q)
   {
      write_sdf_line(dstf,delimiter,*q);
      q = q->next;
   };

   kill_all_qso();
   kill_all_squares();
   kill_all_prefixes();

   fclose(dstf);
};


void export_csv(char *fname)
{
   FILE *dstf;
   struct qso *q;
   char delimiter;
   char s[80];

   if((dstf = fopen(fname,"wt")) == NULL)
   {
      sprintf(s,"File %s cannot be opened!",fname);
      ok_box("Error",s);
      return;
   };

   if(!cntname[0]) return;

   load_prefixes();
   open_contest_file(cntname,act_def->band);

   delimiter = query_delim(';');
   if(delimiter == 27)
   {
      fclose(dstf);
      return;
   };

   make_csv_header(dstf,delimiter);
   q = firstqso;
   while(q)
   {
      write_csv_line(dstf,delimiter,*q);
      q = q->next;
   };

   kill_all_qso();
   kill_all_squares();
   kill_all_prefixes();

   fclose(dstf);
};


void do_import_export()
{
   void *save;
   void *save2;
   char fname[128];
   char sel = 8;
   char ch;
   char result;
   char *cp;

   save = make_win(36,7,75,10,menucol);

   dprint(38,8,menucol,"Export contest (SDF)");
   dprint(38,9,menucol,"Export contest (CSV)");

   dcolor(37,sel,74,sel,menuinvcol);

   do
   {
      ch = getch();
      if(!ch)
      {
         ch = getch();
         switch(ch)
         {
            case 72:
               dcolor(37,sel,74,sel,menucol);
               sel--;
               if(sel<8) sel = 9;
               dcolor(37,sel,74,sel,menuinvcol);
               break;
            case 80:
               dcolor(37,sel,74,sel,menucol);
               sel++;
               if(sel>9) sel = 8;
               dcolor(37,sel,74,sel,menuinvcol);
               break;
         };
      };
   } while(ch!=13 && ch!=27);
   
   if(ch == 13)
   {
      switch(sel)
      {
         case 8:
            if(valid>1) act_def = select_band();
            if(act_def)
            {
               enable_band(act_def, 0);
               save2 = make_win(40,3,70,6,menucol);
               dprint(43,4,menucol,"filename");
               strcpy(fname,act_def->fname);
               cp = strchr(fname,'.');
               if(cp)
               {
                  *cp = 0;
                  strcat(fname,".SDF");
               };
               result = input_string(42,5,26,26,UPCASE,menuinvcol,fname);
               drestore(save2);
               sec_free(save2);
               my_gotoxy(80,lastline+1);
               if(result) export_sdf(fname);
            };
            break;
         case 9:
            if(valid>1) act_def = select_band();
            if(act_def)
            {
               enable_band(act_def, 0);
               save2 = make_win(40,3,70,6,menucol);
               dprint(43,4,menucol,"filename");
               strcpy(fname,act_def->fname);
               cp = strchr(fname,'.');
               if(cp)
               {
                  *cp = 0;
                  strcat(fname,".CSV");
               };
               result = input_string(42,5,36,36,UPCASE,menuinvcol,fname);
               drestore(save2);
               sec_free(save2);
               my_gotoxy(80,lastline+1);
               if(result) export_csv(fname);
            };
            break;
      };
   };

   drestore(save);
   sec_free(save);

};


void disable_band(struct definition *band)
{
};

void enable_band(struct definition *band, unsigned load)
{
   strcpy(cntname,band->fname);
};



void import_contest()
{
};



int main(int argc, char **argv)
{
   char s[81];
   FILE *f;
   char selection;
   char ch;
   char ende;
   char *cp;
   void *save;
   void *save2;
   unsigned oldx, oldy;
   struct pcx_header pcx_h;
   char hlp[128], start_file[128];
   unsigned i;
   unsigned logo = 1;
   unsigned char do_sound;


   // Pfad von AUSWERT.EXE feststellen
   strcpy(fn_path,argv[0]);
   // Programmname abschneiden
   cp = fn_path+strlen(fn_path);
   while((*cp!='\\') && (cp>=fn_path)) cp--;
   cp++;
   *cp = 0;
   start_log("");


   sec_start();
   
   
   // Titellogo ausgeben
   printf("\n");
   for(ch=1;ch<=40;ch++) putch('-');
   printf("\n%s %s, %s\n",PRGNAME,VERSION,DATE);
   printf("(C) 1994-96 by Stefan Pfeiffer, DL1ELY\n");
   for(ch=1;ch<=40;ch++) putch('-');
   printf("\n\n");

   // Bildschirmausgabe initialisieren
   dstart();
  
   // Zuletzt aktiven Kontest bestimmen
   generate_path(fn_path,LAST_CONT_NAME,"\0",hlp);
   if((f = fopen(hlp,"rt"))==NULL)
   {
      defname[0] = 0;
   }
   else
   {
      fgets(defname,256,f);
      if(defname[strlen(defname)-1] == '\n') defname[strlen(defname)-1] = 0;
      fclose(f);
   };

   // Nach evtl. angegebenen Dateinamen suchen
   for(i=1;i<argc;i++)
   {
      strcpy(hlp,argv[i]);
      if(hlp[0]!='-')
      {
         strcpy(defname,hlp);
         strupr(defname);
         cp = strchr(defname,'.');
         if(!cp) strcat(defname,".DES");
      };
   };

  
   // Konfiguration einlesen
   generate_path(fn_path,CONFIG_NAME,"",s);
   read_config(s);
   textcol = get_color("TEXTCOLOR",0);
   titlecol = get_color("TITLECOLOR",0);
   invcol = get_color("INVTEXTCOLOR",0);
   menucol = get_color("MENUCOLOR",0);
   menuinvcol = get_color("MENUINVCOLOR",0);
   activebuttoncol = get_color("ACTBUTTONCOLOR",0);
   passivebuttoncol = get_color("INACTBUTTONCOLOR",0);
   mapcol = get_color("MAPCOLOR",0);
   bordercol = get_color("BORDERCOLOR",0);
   maptextcol = get_color("MAPTEXTCOLOR",0);
   svgamode = get_int("SVGA",0);
   cp = get_str("DEFAULTCALL");
   strcpy(def_call,cp);
   cp = get_str("DEFAULTLOC");
   strcpy(def_loc,cp); 

   cp = get_str("LOGDIR");
   strcpy(logdir,cp);
   if(logdir[strlen(logdir)-1] == '\\') logdir[strlen(logdir)-1] = 0;

   do_blink = get_bool("BLINKING",0);

   gridcol = get_color("GRIDCOLOR",0);
   gridinvcol = get_color("GRIDINVCOLOR",0);

   do_bar_stat = get_bool("BARSTATISTICS",1);
  
   do_sound = get_int("SOUND",0);
   cp = get_str("STARTSOUND");
   generate_path(fn_path,cp,".WAV",start_file);
   printport = get_int("PRINTER_PORT",1);
   printer_type = get_int("PRINTER_TYPE",0);

   always_date = get_bool("ALWAYS_DATE",1);
   always_hour = get_bool("ALWAYS_HOUR",1);

   free_config();

   // Optionen suchen
   for(i=1;i<argc;i++)
   {
      strcpy(hlp,argv[i]);
      if(hlp[0] == '-')
      {
         strupr(hlp);
         cp = (hlp+1);
         if(!strcmp(cp,"NOSVGA")) svgamode = 0;
         if(!strcmp(cp,"NOLOGO")) logo = 0;
      };
   };

   get_graphics();
   switch(graphics)
   {
      case EGA:
         printf("EGA");
         ega_setchars();
         set_blink(do_blink);
         def_cursor(0,7);
         registerfarbgidriver(EGAVGA_driver_far);
         break;
      case VGA:
         printf("VGA");
         vga_setchars();
         set_blink(do_blink);
         def_cursor(0,7);
         registerfarbgidriver(EGAVGA_driver_far);
         if(svgamode)
         {
            printf(", 800x600-Mode 0x%02X",svgamode);
            patch_driver(svgamode);
         };
         break;
      case ATT400:
         printf("AT&T");
         break;
      case HERCMONO:
         printf("Hercules");
         break;  
   };

   printf("\n");
  
   printf("OS: ");
   if(is_windows()) printf("Windows");
   else if(is_os2()) printf("OS/2");
   else printf("DOS");
  
   printf("\n\n");

   if(argc > 1)
   {
      strupr(argv[1]);
      if(!strcmp(argv[1],"REORG")||!strcmp(argv[1],"STATBASE"))
      {
         logo = 0;
      };
   };


   // Momentane Cursorposition merken
   oldx = wherex();
   oldy = wherey();
   oldx--;
   oldy--;

   // Schirm speichern
   save = dsave(0,0,lastrow,lastline);
  
   my_gotoxy(80,lastline+1);
  
   dclrscr(textcol);
   dfill(0,0,lastrow,0,' ',titlecol);
  
   sprintf(s,"%s %s, %s by %s",PRGNAME,VERSION,DATE,AUTHOR);
   dprint(40-(strlen(s)/2),0,titlecol,s);
  
   dfill(0,lastline,lastrow,lastline,' ',titlecol);
  
   date[0] = 0;
   title[0] = 0;
   bands = 0;
   valid = 0;
   firstdef = NULL;
   lastdef = NULL;
  
   if(!load_definitions(defname,date,title))
   {
      defname[0] = 0;
   }
   else
   {
      for(act_def=firstdef;act_def;act_def=act_def->next)
      {
         act_def->found = (f = fopen(act_def->fname,"rb")) != NULL;
         if(act_def->found)
         {
            valid++;
            fclose(f);
         };
      };
   };        
  
   cntname[0] = 0;
  
   strcpy(h.ownloc,def_loc);
   strcpy(h.owncall,def_call);
   h.desc[0] = 0;

   save2 = make_win(5,2,27,15,menucol);

   dprint(7,3,menucol,"Load contest...");
   dprint(7,4,menucol,"Create new contest..");
   dprint(7,5,menucol,"New band..."); 
   dprint(7,6,menucol,"Import contest");

   dfill(6,7,26,7,196,(menucol&0xF0)+YELLOW);

   dprint(7,8,menucol,"Statistics...");
   dprint(7,9,menucol,"Print log...");
   dprint(7,10,menucol,"Print QSL-labels...");
   dprint(7,11,menucol,"Import/Export...");
   dprint(7,12,menucol,"Stations database");

   dfill(6,13,26,13,196,(menucol&0xF0)+YELLOW);

   dprint(7,14,menucol,"Quit programme");
  
  
   if(argc > 1)
   {
      strupr(argv[1]);
      if(!strcmp(argv[1],"REORG"))
      {
         reorganisation(0);
      
         switch(graphics)
         {
            case EGA:
               restore_ega_charset();
               set_blink(BLINKING);
               def_cursor(6,7);
               break;
            case VGA:
               restore_vga_charset();
               set_blink(BLINKING);
               def_cursor(6,7);
               break;
         };
  
         dend();

         drestore(save);
         sec_free(save);

         my_gotoxy(oldx,oldy);

         textcolor(WHITE);
         textbackground(BLACK);

         printf("\nThis programme is (C) 1994-96 by Stefan Pfeiffer, DL1ELY\n");
         printf("Please obey the rules of LICENCE.DOC\n\n");
  
         exit(0);
      };


      if(!strcmp(argv[1],"STATBASE"))
      {
         do_browse_stations();
      
         switch(graphics)
         {
            case EGA:
               restore_ega_charset();
               set_blink(BLINKING);
               def_cursor(6,7);
               break;
            case VGA:
               restore_vga_charset();
               set_blink(BLINKING);
               def_cursor(6,7);
               break;
         };
  
         dend();

         drestore(save);
         sec_free(save);

         my_gotoxy(oldx,oldy);

         textcolor(WHITE);
         textbackground(BLACK);

         printf("\nThis programme is (C) 1994-96 by Stefan Pfeiffer, DL1ELY\n");
         printf("Please obey the rules of LICENCE.DOC\n\n");
  
         exit(0);
      };

   };
  
  
   act_def = firstdef;
   disp_cnt_data();
  
   ende = 0;
   selection = 3;
   dcolor(6,selection,26,selection,menuinvcol);
   while(!ende)
   {
      dfill(0,lastline,lastrow,lastline,' ',titlecol);
    
      do
      {
         ch = getch();
         if(!ch)
         {
            ch = getch();
       
            switch(ch)
            {
               case 104: // ALT-F1
                  do_mem_stat();
                  break;

               case 72: // Cursor up
                  dcolor(6,selection,26,selection,menucol);
                  selection--;
                  if(selection < 3) selection = 14;
                  if(selection==13) selection--;
                  if(selection==7) selection--;
                  dcolor(6,selection,26,selection,menuinvcol);
                  break;
          
               case 80: // Cursor down
                  dcolor(6,selection,26,selection,menucol);
                  selection++;
                  if(selection > 14) selection = 3;
                  if(selection==13) selection++;
                  if(selection==7) selection++;
                  dcolor(6,selection,26,selection,menuinvcol);
                  break;
          
               case 45: // Alt-X
                  ende = 1;
                  ch = 13;
                  break;
            };
         };
      } while(ch!=13);
            
      if(!ende)
      {
         switch(selection)
         {
            case 3:
               do_load_contest();
               break;
            case 4:
               make_contest_file();
               break;
            case 5:
               new_band();
               break;
            case 6:
               import_contest();
               break;
            case 8:
               if(valid)
               {
                  if(valid>1) act_def = select_band();
                  if(act_def)
                  {
                     enable_band(act_def,0);
                     if(h.qso_cnt) do_stat();
                  }
               }
               break;
            case 9:
               if(valid)
               {
                  if(valid>1) act_def = select_band();
                  if(act_def)
                  {
                     enable_band(act_def,0);
                     do_logprint();
                  }
               }
               break;
            case 10:
               do_qsl_print();
               break;
            case 11:
               do_import_export();
               break;
            case 12:
               do_stations();
               break;
            case 14:
               ende = 1;
               break;
         };
      };
   };

   drestore(save2);
   sec_free(save2);

   kill_definitions();
  
   switch(graphics)
   {
      case EGA:
         restore_ega_charset();
         set_blink(BLINKING);
         def_cursor(6,7);
         break;
      case VGA:
         restore_vga_charset();
         set_blink(BLINKING);
         def_cursor(6,7);
         break;
   };
  
   dend();

   drestore(save);
   sec_free(save);

   my_gotoxy(oldx,oldy);

   textcolor(WHITE);
   textbackground(BLACK);

   printf("\nThis programme is (C) 1994-96 by Stefan Pfeiffer, DL1ELY.\n");
   printf("Please obey the rules of LICENCE.DOC\n\n");

   return(0);
};

