// Output from p2c 1.21alpha-07.Dec.93, the Pascal-to-C translator
// From input file "/var/home/pantec/src/turbo/ucdas/ucdas.p"


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//               ProgramName:   UCDAs.eXe
//               Used for:
//               Compiler:      Turbo_Pascal 6.0
//               CopyRigth by:  Thomas Pantzer
//                              Kurt Tucholskistr.8
//                              O-7030  Leipzig
//               Date:          8.3.1993
//_____________________________________________________________________________



#include <p2c/p2c.h>

#include <p2c/exithandler.h>

#ifndef NCRT_H
#include "/software/include/p2c/ncrt.h"
#endif

#ifndef KEYCONST_H
#include "/software/include/p2c/keyconst.h"
#endif

#ifndef STRTYPE_H
#include "/software/include/p2c/strtype.h"
#endif

#ifndef RS232B_H
#include "/software/include/p2c/rs232b.h"
#endif

#ifndef TVERSION_H
#include "/software/include/p2c/tversion.h"
#endif

#ifndef TMENU_H
#include "/software/include/p2c/tmenu.h"
#endif

#ifndef DASUNIT2_H
#include "/software/include/p2c/dasunit2.h"
#endif

#ifndef FEXIST_H
#include "/software/include/p2c/fexist.h"
#endif

#ifndef XDOS_H
#include "/software/include/p2c/xdos.h"
#endif


#define MaxMenu_        9
#define MaxWindow       3
#define MaxCodeBuffer   65536L
#define MaxReceiveBuf   8192

#define MaxResFiles     9


typedef uchar LngStrArray[8193];

typedef uchar Byte2048[MaxReceiveBuf + MaxCdByte + 1];
typedef uchar Byte8192[MaxCodeBuffer + MaxCdByte + 1];

class TextMenuType2 : public TextMenuType {
public:
  virtual void Idle(void);
  void ReadHex(uchar nr, unsigned short &i);
};


#include "ucconfig.h"


static char FileNmSym[256] = "";
static char FileNmLst[256] = "";
static char FileNmRcv[256] = "";
static char FileNmBak[256] = "";
static char FileNmObj[256] = "";


static char FileNmCde[256] = "";

static boolean Echo = false;
static boolean NoHandShake = false;
static boolean Ctrl_Break = false;

static unsigned short SendCount = 0;
static unsigned short WriteCount = 0;
static unsigned short TextLines = 0;

static char ResFile[MaxResFiles][5] = {
  "0", "1", "2", "COM3", "COM4", "LPT1", "LPT2", "LPT3", "AUX"
};

TextMenuType2 *Menu[MaxMenu_];

unsigned short rd, ComHandle, MaxRead, Index;

FILE *Datei;
ByteSec CdBytes;

uchar *EditBuf;
uchar *RecBuf;

TextWindowType Win[MaxWindow];
TextBrowseType Browser;


uchar SaveLines;
int i;
char ch;
char Datei_NAME[_FNSIZE];


static void TrimFileString(char *p, char *n, char *e, char *st,
			   const int MAX_st)
{
  char STR_[256];

  if (*st == '\0')
    strcpy(st, n);
  if (strpos2(st, ".", 1) == 0)
    strcat(st, e);
  if (strpos2(st, "/", 1) == 0)
    sprintf(st, "%s%s", p, strcpy(STR_, st));
}


// $I KeyBuffr.pas
// $I ToNumber.pas
// $I FExists.pas

static void WriteConfig(void)
{
  long w;
  char STR_[92];

  w=(&configIndex3) - (&configIndex1);

  sprintf(STR_, "%s/.ucdasrc", getenv("HOME"));

#ifdef DEBUG  
  fprintf(stderr,"size of config-rec: %d\trcfile: %s\n",w,STR_);
#endif  

  strcpy(Datei_NAME, STR_);
  if (Datei != NULL)
    Datei = freopen(Datei_NAME, "w+b", Datei);
  else
    Datei = fopen(Datei_NAME, "w+b");

  _SETIO(Datei != NULL, FileNotFound);
  _SETIO(fwrite(&configIndex1, w, 1, Datei) == 1, FileWriteError);
  if (Datei != NULL)
    fclose(Datei);
  Datei = NULL;
}


static void Toggle(boolean &b)
{
  if (b)
    b = false;
  else
    b = true;
}



void Init_W0_W2()
{
  int i;
  i=(_Display_.Width*3/4)+5;

  Win[0].Init(2, 3, i-2, _Display_.Height - 2, 
                 VC[0][0], VC[0][1], VC[0][2], VC[0][3], Msg[0]);

  Win[2].Init(i+1, 3, _Display_.Width-1, _Display_.Height - 2, 
                 VC[2][0], VC[2][1], VC[2][2], VC[2][3], Msg[2]);
}

static void InitMenuLo(void)
{
  char STR_[90];

  MenuInitRec[4].Y = _Display_.Height; // + 1;

  Init_W0_W2();
  
  Win[1].Init(25, 8, 55, 11, VC[1][0], 
                 VC[1][1], VC[1][2], VC[1][3], Msg[1]);

  sprintf(STR_, "%sucdas.help", LIBPATH);

  
  Browser.Init(2, 2, _Display_.Width-5, _Display_.Height - 2, STR_, Msg[19]);

  Browser.Frame.BC = VC[3][0];
  Browser.Frame.TitleColor = VC[3][1];
  Browser.Frame.NormColor = VC[3][2];
}


static void InitMenus(void)
{
  InitMenuLo();

  for (i = 1; i <= MaxMenu_; i++)
    {
     MenuInitRecType *WITH_ = &MenuInitRec[i-1];
     Menu[i-1] = new TextMenuType2;
     Menu[i-1]->Init(WITH_->X, WITH_->Y, WITH_->NC, WITH_->HiC, WITH_->AsC,
		     WITH_->Tp, WITH_->ST);
    }

//  MenuInitRec[0].X = _Display_.Width; // + 1;
//  MenuInitRec[4].X = _Display_.Width; // + 1;
}


static void ReInitCRT(void)
{
  InitMenus();


  Win[0].Show();
  Win[2].Show();
  Menu[0]->Show();
  Menu[4]->Show();
}


static void ToggleEgaLines(void)
{
  for (i = MaxMenu_; i >= 1; i--)
    {
     Menu[i-1]->Done();
     delete Menu[i-1];
    }

  for (i = MaxWindow; i >= 1; i--)
    Win[i-1].Done();

  ReInitCRT();
}


static void GetReceive(void)
{
  RecBuf[SendCount] = Int14Call->Receive();
  if (!Int14Call->LastError)
    SendCount = (SendCount + 1) % MaxReceiveBuf;
  //
  //if WriteCount=SendCount
  //   then write(^G);
  //
}



static void UCDAsExit(void)
{
  char STR_1[92];

  for (i = MaxMenu_; i >= 1; i--)
    {
     Menu[i-1]->Done();
     delete Menu[i-1];
    }

  for (i = MaxWindow; i >= 1; i--)
    Win[i-1].Done();


  if (BackupOption)
    {
     sprintf(STR_1, "./ucdas-bak00%s", ObjExt);
     strcpy(Datei_NAME, STR_1);


     if (Datei != NULL)
       Datei = freopen(Datei_NAME, "w+b", Datei);
     else
       Datei = fopen(Datei_NAME, "w+b");

     _SETIO(Datei != NULL, FileNotFound);
     if (P_ioresult == 0)
       {
	unsigned short wr = fwrite(EditBuf, 1, MaxCodeBuffer, Datei);
	if (Datei != NULL)
	  fclose(Datei);
	Datei = NULL;
       }
     sprintf(STR_1, "./ucdas-bak01%s", SymExt);


     WriteConfig();
#ifdef DEBUG
     fprintf(stderr,"pre-backup file [%s]\n",STR_1);
#endif
     WriteSymbols(STR_1);
    }
#ifdef DEBUG
   fprintf(stderr,"post-backup\n");
#endif

  delete [] EditBuf;
  delete [] RecBuf;

  Int14Call->Done();

  delete Int14Call;

  CursorOn();
// p2c: /var/home/pantec/src/turbo/ucdas/ucdas.p, line 403:
// * Warning: Symbol 'CURSORON' is not defined [221]

//  _Escape(0);

  if (Datei != NULL)
    fclose(Datei);

#ifdef DEBUG
  fprintf(stderr,"UCDAs.Done\n");
#endif

  exit(EXIT_SUCCESS);
}



static void ReadConfig(void)
{
  char s[256];
  char STR_[256];
  char STR_1[256];
  char STR_2[92];
  //  Savelines:=Mem[0:$484]+1;

  //
  //w:=ofs(ConfigIndex1);
  //P:=ptr(seg(ConfigIndex1),w);
  //w:=ofs(ConfigIndex3)+1-w;
  //
  unsigned short w = &configIndex3 - &configIndex1 + 1;
  
//  strcpy(s,GetEnv("UCD"));
//  sprintf(STR_1, "UCDAS_%s.ovl", s);
//  if (FileExists(STR_1))
//    {
//     sprintf(STR_, "%sUCDAS_%s.ovl", ProgSelfPath, s);
//     strcpy(Datei_NAME, STR_);
//    }
//  else
//    {
//     sprintf(STR_2, "%sUCDAS_0.ovl", ProgSelfPath);
//    }
  sprintf(Datei_NAME,"%s/.ucdasrc",getenv("HOME"));

  if (FileExists(Datei_NAME) == FALSE) 
      sprintf(Datei_NAME,"%s/sys.ucdasrc",LIBPATH);

//  strcpy(Datei_NAME, STR_1);

  if (Datei != NULL)
    {
     Datei = freopen(Datei_NAME, "r+b", Datei);
    }
  else
    Datei = fopen(Datei_NAME, "r+b");
  _SETIO(Datei != NULL, FileNotFound);
  unsigned short r = fread(&configIndex1, 1, w, Datei);
  P_ioresult = 0;
  if (Datei != NULL)
    fclose(Datei);
  Datei = NULL;

  if (P_argc > 1)
      FSplit(P_argv[1],BinLoadPath,ObjName,ObjExt);
   else
     {
      if (BackupOption)
         { 
          FSplit(LastObject,BinLoadPath,ObjName,ObjExt);
         } 
     }

  TrimFileString(BinLoadPath, ObjName, ObjExt, FileNmObj, 255);

#ifdef DEBUG
  fprintf(stderr,"FileNmObj: %s\n",FileNmObj);
#endif

  if (*SymName == '\0')
    strcpy(SymName, ObjName);

  TrimFileString(BinLoadPath, SymName, SymExt, FileNmSym, 255);

  if (*RcvName == '\0')
    strcpy(RcvName, ObjName);

  TrimFileString(BinLoadPath, RcvName, RcvExt, FileNmRcv, 255);

  if (*LstName == '\0')
    strcpy(LstName, ObjName);

  TrimFileString(BinLoadPath, LstName, LstExt, FileNmLst, 255);

  if (*BakName == '\0')
    strcpy(BakName, ObjName);

  TrimFileString(BinLoadPath, BakName, BakExt, FileNmBak, 255);

  if (*CdeName == '\0')
    strcpy(CdeName, "68HC11");

  TrimFileString(CdePath, CdeName, CdeExt, FileNmCde, 255);

  if (MaxSendCount > MaxCodeBuffer)
    MaxSendCount = MaxCodeBuffer & 0x0ffff;
}


static char *ModeString(char *__Result__, boolean b)
{
  if (b)
    return strcpy(__Result__, Msg[1]);
  else
    return strcpy(__Result__, Msg[2]);
}


static void About(void)
{
  char TEMP_;

  Menu[1]->Show();
  do
    {
    } while (!(TEMP_ = Menu[1]->GetEvent(),
	       TEMP_ == '\015' || 
               TEMP_ == fmRightButton));
  Menu[1]->Hide();
}


static void ErrorMsg(char *st_)
{
  char st[256];
  char STR_1[256];

  strcpy(st, st_);
  Win[1].Show();
  sprintf(STR_1, "%s\n%s", st, Msg[4]);
  Win[1].WriteStr(STR_1);

// discard pending events
//  while (Menu[0]->EventAvail)
//    ch = Menu[0]->GetEvent();
  
  ch='~';
  
  do
    {
     if (Menu[0]->EventAvail)
        ch = Menu[0]->GetEvent();

     if (ch == '\0')
       {
         ch = Menu[0]->GetEvent();
         if (ch == fmAltXKey)
           ExitProc.Halt();
       }    
    } while (ch != '\015' && ch != '\033' && ch != fmRightButton);

  Win[1].Hide();
}


static void UpDateMonitor(void)
{
  while (WriteCount != SendCount && !Menu[7]->EventAvail())
    {
     Win[2].WriteChr(RecBuf[WriteCount]);
     switch (RecBuf[WriteCount])
       {

	 //      13: writeLn;
	 case 8:
	Win[2].ClrEol();
	break;
	//       9: Win[3].WriteChr('        ');
       }
     WriteCount = (WriteCount + 1) % MaxReceiveBuf;
    }
}

void TextMenuType2::Idle(void)
{
  //  while (Int14Call^.Status and $00100)>0 do
  GetReceive();
  UpDateMonitor();
  
  TextMenuType::Idle();
}


void TextMenuType2::ReadHex(uchar nr, unsigned short &i)
{
  unsigned short j=0;
  char s[256],s2[256];

  if (MenuType != VertAsk)
    return;

  sprintf(s,"0x0%x",i);
  Display(nr, s, fmInputColor);

  strcpy(s,GetString(s2, nr));  
#ifdef DEBUG
  fprintf(stderr,"s: ]%s[\ts2: %s\n",s,s2);
#endif

  int c;
  
  if (strlen(s)>0)
     {
      c = sscanf(s, "0x0%c%c%c%c" , &s2[0],&s2[1],&s2[2],&s2[3]);
  if (0 < c)
     {
      s2[c]='\0';
      char f_error=FALSE;
      
      for (short k=0; k<c; k++)
         {
//          if (s2[k] == ' ')
//              c=k; 
//            else 
              if (!(('0' <= s2[k]) && (s2[k] <= '9')))
                 {
                  if ((('a' <= s2[k]) && (s2[k] <= 'f')) || (('A' <= s2[k]) && (s2[k] <= 'F')))
                      s2[k]= (s2[k] & 0x07) + 9;
                   else
                      f_error=TRUE;
                 }     
         } 
      
      if (f_error == FALSE)
         {
          j=0;
          for (short k=0; k<c; k++)
             {
              j = (j << 4);
              j = j | (s2[k] & 0x0f);
#ifdef DEBUG
              fprintf(stderr,"j: %d\ts2[%d]: 0x0%x\n",j,k,s2[k] & 0x0f);
#endif
             }
          i = j;
         } 
       else ErrorMsg(Msg[11]); 
     } 
   else ErrorMsg(Msg[11]);  
   }

#ifdef DEBUG
  fprintf(stderr,"c: %d\ti: 0x0%x\tj: %d\n",c,i);
#endif

  sprintf(s,"0x0%x",i);
  Display(nr, s, AskColor);
//  TextAttr = NormColor;
}




static boolean Scrollock(void)
{
  return false;
  //
  //if Mem[0:($417)] and $10 >0
  //   then begin
  //          b:=Mem[0:($417)] and $ef;
  //          Mem[0:($417)]:=b;
  //
  //          b:=(b shr 4) and 7;
  //          Port[$64]:=$ed;
  //          delay(10);
  //          Port[$64]:=b;
  //
  //          Scrollock:=true;
  //        end;
  //
}


static void SendBuffer(void)
{
  unsigned short j = 0;
  char STR_[256];

  Menu[2]->ReadWord(3, StartPos);

  uchar FORLIM_ = strlen(SendPrefix);
  for (uchar i = 1; i <= FORLIM_; i++)
    Int14Call->Send(SendPrefix[i-1]);

  while (j < MaxSendCount && !Ctrl_Break)
    {
     unsigned short k = SendCount;
     Delay(1);

     Int14Call->Send(EditBuf[j + StartPos]);
     do
       {
	GetReceive();
	Ctrl_Break = Scrollock();
       } while (!(k != SendCount || Ctrl_Break));

     j++;

     if ((j & 15) == 0)
       Menu[2]->Display(3, WToString(STR_, j), Menu[2]->AskColor);
    }

  Ctrl_Break = false;

  putchar('\007');
}


static void ResetReceivePtr(void)
{
  WriteCount = 0;
  SendCount = 0;
}


static void ReadInFile(void)
{
  if (!Menu[2]->ReadStr(4, FileNmRcv))
    {
     *FileNmRcv = '\0';
     TrimFileString(ProgSelfPath, RcvName, RcvExt, FileNmRcv, 255);
     return;
    }

  TrimFileString(ProgSelfPath, RcvName, RcvExt, FileNmRcv, 255);
  Menu[2]->Display(4, FileNmRcv, Menu[2]->AskColor);
  char InFile_NAME[256]; strcpy(InFile_NAME, FileNmRcv);
  FILE *InFile = fopen(InFile_NAME, "w+b");
  _SETIO(InFile != NULL, FileNotFound);
  if (P_ioresult == 0)
    {
     WriteCount = SendCount;
     do
       {
	GetReceive();

	while (WriteCount != SendCount)
	  {
	   if (WriteCount < 2048)
	     WriteCount++;
	   else
	     WriteCount = 1;
	   fwrite(&RecBuf[WriteCount], sizeof(uchar), 1, InFile);
	  }
       } while (!Menu[2]->EventAvail());
     if (InFile != NULL)
       fclose(InFile);
     InFile = NULL;
     ch = Menu[2]->GetEvent();
    }
  else
    ErrorMsg(Msg[3]);
  if (InFile != NULL)
    fclose(InFile);
}


static void WriteFile(void)
{
  FILE *SaveFile = NULL;
  char SaveFile_NAME[_FNSIZE];

  *FileNmBak = '\0';
  TrimFileString(BinLoadPath, BakName, BakExt, FileNmBak, 255);

  Menu[5]->Show();
  Menu[5]->Display(1, FileNmBak, Menu[5]->AskColor);
  if (!Menu[5]->ReadStr(1, FileNmBak))
    {
     Menu[5]->Hide();
     ch = '~';
     return;
    }
  TrimFileString(BinLoadPath, BakName, BakExt, FileNmBak, 255);
  Menu[5]->Display(1, FileNmBak, Menu[5]->AskColor);
  Menu[5]->ReadHex(2, StartPos);
  Menu[5]->ReadWord(3, MaxBlocks);
  do
    {
     ch = Menu[5]->GetEvent();
     switch (ch)
       {
	      case '\0': switch (Menu[5]->GetEvent())
                 	   {
                                   case '-': ExitProc.Halt();
                            break; case ';': Browser.Exec();
                           }
	break; case '#': About();
        break; case 'F': if (Menu[5]->ReadStr(1, FileNmBak))
                            {
                             TrimFileString(BinLoadPath, BakName, BakExt, FileNmBak, 255);
                             Menu[5]->Display(1, FileNmBak, Menu[5]->AskColor);
                            }
	break; case 'S': Menu[5]->ReadHex(2, StartPos);
	break; case 'B': Menu[5]->ReadWord(3, MaxBlocks);
       } // case
    } while ((uchar)ch >= 32 || ((1L << ch) & 0x8002000L) == 0);

  if (ch == '\015')
    {
     strcpy(SymName, BakName);
     *FileNmSym = '\0';
     TrimFileString(BinLoadPath, SymName, SymExt, FileNmSym, 255);
     TrimFileString(BinLoadPath, BakName, BakExt, FileNmBak, 255);

     Menu[5]->Display(1, FileNmBak, Menu[5]->AskColor);
     WriteSymbols(FileNmSym);

     strcpy(SaveFile_NAME, FileNmBak);
     SaveFile = fopen(SaveFile_NAME, "w+b");
     _SETIO(SaveFile != NULL, FileNotFound);
     if (P_ioresult == 0)
       {
	unsigned short s = MaxBlocks * 128 - StartPos;
	unsigned short w = fwrite(&EditBuf[StartPos], 1, s, SaveFile);
	if (SaveFile != NULL)
	  fclose(SaveFile);
	SaveFile = NULL;
       }
     else
       ErrorMsg(Msg[3]);
    }
  Menu[5]->Hide();
  ch = '~';
  if (SaveFile != NULL)
    fclose(SaveFile);
}


static void NewFileName(void)
{
  DirStr p;
  NameStr n;
  ExtStr e;
  char STR_[256];
  char STR_1[256];

  *FileNmObj = '\0';
  TrimFileString(BinLoadPath, ObjName, ObjExt, FileNmObj, 255);

  if (!Menu[2]->ReadStr(1, FileNmObj))
    {
     *FileNmObj = '\0';
     TrimFileString(BinLoadPath, ObjName, ObjExt, FileNmObj, 255);
     return;
    }

  FSplit(FileNmObj, p, n, e);

  strcpy(SymName, n);
  strcpy(LstName, n);
  strcpy(RcvName, n);
  strcpy(BakName, n);
  strcpy(ObjName, n);
  
  if (strlen(p) > 0)
     strcpy(BinLoadPath,p);
     
  TrimFileString(BinLoadPath, ObjName, ObjExt, FileNmObj, 255);

  Menu[2]->Display(1, FileNmObj, Menu[2]->AskColor);

#ifdef DEBUG
  fprintf(stderr,"post Display[%s]\n",FileNmObj); 
#endif
  
  for (long int i = 0; i < MaxCodeBuffer; i++)
    EditBuf[i] = 0;

  ClearAllLabels();
  strcpy(SymName, ObjName);
  *FileNmSym = '\0';
  TrimFileString(BinLoadPath, SymName, SymExt, FileNmSym, 255);

#ifdef DEBUG
  fprintf(stderr,"pre ReadSymbols(%s)\n",FileNmSym); 
#endif

  if (FileExists(FileNmSym))
     ReadSymbols(FileNmSym);

  Index = 0;
  TextLines = 0;

  if (Datei != NULL)
    Datei = freopen(FileNmObj, "r+b", Datei);
  else
    Datei = fopen(FileNmObj, "r+b");
  _SETIO(Datei != NULL, FileNotFound);
  if (P_ioresult != 0)
    {
     ErrorMsg(Msg[6]);
     return;
    }
  MaxRead = fread(EditBuf, 1, MaxCodeBuffer, Datei);
  if (Datei != NULL)
    fclose(Datei);
  Datei = NULL;
  sprintf(STR_1, "%s%s", WToString(STR_, MaxRead), Msg[5]);
  Menu[2]->Display(1, STR_1, Menu[2]->AskColor);
  
  if (MaxRead > 0)
     strcpy(LastObject,FileNmObj);
}




static void SeekPos(void)
{
  Menu[2]->Show();

  Menu[2]->ReadHex(2, Index);
  if (Index > MaxRead)
    Index = MaxRead;

  Menu[2]->Hide();
}


static char *StUpCase(char *__Result__, char *st)
{
  char st2[256];

  strcpy(st2, st);
  uchar FORLIM_ = strlen(st2);
  for (uchar i = 1; i <= FORLIM_; i++)
    st2[i-1] = toupper(st2[i-1]);
  return strcpy(__Result__, st2);
}


static void UpdateStatus(void)
{
  Str6 STR_;
  char STR_1[256];

  Menu[4]->Display(1, HexaWord(STR_, Index/* + daBaseAddr*/), Menu[4]->AskColor);
  Menu[4]->Display(2, WToString(STR_1, TextLines), Menu[4]->AskColor);
  Menu[4]->Display(3, WToString(STR_1, HighestLabel)   , Menu[4]->AskColor);
  Menu[4]->Display(4, WToString(STR_1, MaxRead)        , Menu[4]->AskColor);
//  Menu[4]->Display(4, WToString(STR_1, AllocatedLabels), Menu[4]->AskColor);
//  Menu[4]->Display(6, WToString(STR_1, daBaseAddr), Menu[4]->AskColor);
}



static char *nextCodeLine(char *__Result__)
{
  unsigned short l;
  char st[256];
  char STR_[256];
  Str3 STR_1;
  Str6 STR_2;

  if (Index < MaxRead)
    {
     unsigned short j = (Index/* + daBaseAddr*/) % 65536L;
     unsigned short k = j;

     if (GetTable(j, st, 255))
       {
	sprintf(st, "\n%s", strcpy(STR_, st));

	uchar n = j - k;
	switch (n)
	  {
	           case 1: strcat(st, HexaByte(STR_1, EditBuf[Index], true));
	   break;  case 2: {
                            l = (EditBuf[Index] << 8) & 0xff00L;
                            l |= EditBuf[Index+1];
                            strcat(st, HexaWord(STR_2, l));
                           } 
	   break; default: {
                            strcat(st, "'");
                            for (l = 0; l <= n - 1; l++)
                               sprintf(st + strlen(st), "%c", EditBuf[Index + l]);

                            strcat(st, "'");
                           } 
	  }
       }
     else
       {
	for (uchar i = 0; i <= 4; i++)
	  CdBytes[i] = EditBuf[Index + i];

	GetCode(st, CdBytes, j);

#ifdef DEBUG
//        fprintf(stderr,"NextCodeLine(GetCode,%s,%d)\n",st,j);
#endif
       }

     Index = j/* - daBaseAddr*/;
     TextLines++;
     return strcpy(__Result__, st);
    }
    
  Index = 0;
  TextLines = 0;
  *st = '\0';
  return strcpy(__Result__, st);
}



static void PassOne(boolean Ot)
{
  char stv[256];
  FILE *t = NULL;
  boolean out = Ot;
//  boolean prn = false;
  char STR_[256];
  char t_NAME[_FNSIZE];

  if (out)
    {
     *stv = '\0';
     *FileNmLst = '\0';
     TrimFileString(BinLoadPath, LstName, LstExt, FileNmLst, 255);
     if (Menu[2]->ReadStr(5, FileNmLst))
       {
	strcpy(FileNmLst, StUpCase(STR_, FileNmLst));

	boolean res = false;

//	if (strcmp(FileNmLst, "PRN") == 0)
//	  prn = true;
//	else
//	  {
	   unsigned short i = 0;
	   while (i < MaxResFiles && !res)
	     {
	      if (strcmp(FileNmLst, ResFile[i]) == 0)
		res = true;
	      i++;
	     }
//	  }

	if (!res)
	  TrimFileString(BinLoadPath, LstName, LstExt, FileNmLst, 255);

	Menu[2]->Display(5, FileNmLst, Menu[2]->AskColor);

//	if (strcmp(StUpCase(STR_, FileNmLst), "PRN") == 0)
//	  prn = true;
//	else
//	  {
	   strcpy(t_NAME, FileNmLst);
	   t = fopen(t_NAME, "w");
	   _SETIO(t != NULL, FileNotFound);
//	  }
       }
     else
       out = false;
    }
  else
    {
     Win[0].Clear();
     Win[0].WriteStr(Msg[16]);
    }

  Index = 0;
  TextLines = 0;

  do
    {
     GetReceive();
     UpdateStatus();
     nextCodeLine(stv);

     if (out)
//       if (prn)
//	 _SETIO(fprintf(lst, "%s\n", stv) >= 0, FileWriteError);
//       else
	 _SETIO(fprintf(t, "%s\n", stv) >= 0, FileWriteError);
     TextLines++;

     if (Menu[0]->EventAvail())
       ch = Menu[0]->GetEvent();

     if (ch == '\033')
       {
	ch = '\0';
	TextLines = 0;
	if (!out)
	  return;

//	if (prn)
//	  return;

	if (t != NULL)
	  fclose(t);
	return;
       }
    } while (Index < MaxRead);

  TextLines = 0;
  if (out)
    {
//     if (prn)
//       _SETIO(putc('\f', lst) >= 0, FileWriteError);
//     else
       _SETIO(putc('\f', t) >= 0, FileWriteError);
//     if (!prn)
//       {
	if (t != NULL)
	  fclose(t);
	t = NULL;
//       }
    }
  if (t != NULL)
    fclose(t);
}


static void FileMenu(void)
{
  char STR_[256];

  Menu[2]->Show();
  Menu[2]->Display(1, FileNmObj, Menu[2]->AskColor);
  Menu[2]->Display(2, WToString(STR_, Index), Menu[2]->AskColor);
  Menu[2]->Display(3, WToString(STR_, StartPos), Menu[2]->AskColor);
  Menu[2]->Display(4, FileNmRcv, Menu[2]->AskColor);
  Menu[2]->Display(5, FileNmLst, Menu[2]->AskColor);

  do
    {
     ch = Menu[2]->GetEvent();
     switch (ch)
       {
	      case '\0': switch (Menu[2]->GetEvent())
                 	   {
                                   case     '-': ExitProc.Halt();
                            break; case     ';': Browser.Exec();
                            break; case fmF2Key: WriteFile();
                            break; case fmF3Key: NewFileName();
                           }
        break; case '#': About();                   
	break; case 'L': NewFileName();
	break; case 'S': WriteFile();
	break; case 'D': SendBuffer();
	break; case 'R': ReadInFile();
	break; case 'I': PassOne(true);
	break; case 'E': Menu[2]->ReadWord(2, Index);
                	 if (Index > MaxRead)
                            Index = MaxRead;
	break; case 'A': ExitProc.Halt();
       } // case
       doupdate();
    } while (ch != '\033' && ch != fmRightButton);

  Menu[2]->Hide();
  ch = '~';
}


static void InitSerial(void)
{
  static char ParStr[4][5] = {
    "none", "odd", "none", "even"
  };

  unsigned short D;
  uchar Parity, Stop, Data;
  char St[256];
  char STR_[256];
  char STR_2[256];
  char STR_3[256], STR_4[256];

//  ReadPreSet(D, Data, Parity, Stop);
  D      = Standard.Baud;
  Data   = Standard.Data;
  Parity = Standard.Parity;
  Stop   = Standard.Stop;

  Menu[6]->Show();
  long long_ = 115200L / D;
  sprintf(St, "%s/%s/%s/%s",
	  LToString(STR_, long_), BToString(STR_2, Data), ParStr[Parity],
	  BToString(STR_3, Stop));
  Menu[6]->Display(5, St, Menu[6]->AskColor);


/*
  Menu[6]->ReadWord(1, D);
  Menu[6]->ReadByte(2, Data);
  Menu[6]->ReadByte(3, Parity);
  Menu[6]->ReadByte(4, Stop);
*/
     if (D == 0)
       {
	ErrorMsg(Msg[22]);
	D = 1;
       }

     sprintf(St, "%s/%s/%s/%s",
	     LToString(STR_, 115200L / D), BToString(STR_3, Data),
	     ParStr[Parity], BToString(STR_4, Stop));

     Menu[6]->Display(5, St, Menu[6]->AskColor);

  do
    {
     ch = Menu[6]->GetEvent();
     switch (ch)
       {

	 case '\0':
	switch (Menu[6]->GetEvent())
	  {
	          case '-': ExitProc.Halt();
           break; case fmF2Key: WriteFile();
	   break; case ';': Browser.Exec();
	  }
	break; case 'B': Menu[6]->ReadWord(1, D);
	break; case 'D': Menu[6]->ReadByte(2, Data);
	break; case 'P': Menu[6]->ReadByte(3, Parity);
	break; case 'S': Menu[6]->ReadByte(4, Stop);
        break; case '#': About();                   
	break; case 'F': {
                          D = Standard.Baud;
                          Data = Standard.Data;
                          Parity = Standard.Parity;
                          Stop = Standard.Stop;
                         } 
//	break; case 'A': ReadPreSet(D, Data, Parity, Stop);
       } // case
       
       if (D == 0)
         {
	  ErrorMsg(Msg[22]);
          D = 1;
         }

       sprintf(St, "%s/%s/%s/%s",
	     LToString(STR_, 115200L / D), BToString(STR_3, Data),
	     ParStr[Parity], BToString(STR_4, Stop));

       Menu[6]->Display(5, St, Menu[6]->AskColor);

       doupdate();       
    } while ((uchar)ch >= 32 || ((1L << ch) & 0x8002000L) == 0);


  if (ch != '\033')
    {
     if (Parity >= 32 || ((1L << Parity) & 0xf) == 0)
       Parity = 2;

     if (D == 0)
       {
	ErrorMsg(Msg[22]);
	D = 1;
       }

     SetBaud(D);
     SetLocal(Data, Parity, Stop);
    }
  Menu[6]->Hide();
  ch = '~';
}


static void Monitoring(void)
{
  unsigned short Report = 0;
  FILE *f;
  Str120 STR_;
  char f_NAME[_FNSIZE];

  Menu[4]->Hide();
  Menu[0]->Hide();
  Win[2].Done();
  Win[0].Done();

  Win[0].Init(2, 16, _Display_.Width-2, _Display_.Height - 2, VC[0][0], VC[0][1], VC[0]
	      [2], VC[0][3], Msg[7]);
  Win[2].Init(2, 3, _Display_.Width-2, 13, VC[2][0], VC[2][1], VC[2][2], VC[2][3], Msg[8]);

  Win[0].Show();
  Win[2].Show();
  Menu[4]->Show();
  Menu[7]->Show();

  do
    {
	UpdateStatus();
	ch = Menu[7]->GetEvent();
	if (ch == '\0')
	  {
	   ch = Menu[7]->GetEvent();
	   switch (ch)
	     {
	             case       ';': Browser.Exec();
	      break; case       '-': ExitProc.Halt();
//              break; case   fmF2Key: WriteFile();
	      break; case  fmF10Key: ch=fmAltF10Key;
              break; case       '<':
	             case    '\037': {
                                      if (Report > MaxCodeBuffer)
                                         Report = 0;
                                      for (i = 0; i <= 4; i++)
                                         CdBytes[i] = EditBuf[Report + i];
                                      Win[0].ScrollUp(GetCode(STR_, CdBytes, Report));
                                     } 
	      break; case fmAltUKey: {
                                      if (Report > MaxReceiveBuf)
                                	 Report = 0;
                                      for (i = 0; i <= 4; i++)
                                         CdBytes[i] = RecBuf[Report + i];
                                      Win[0].ScrollUp(GetCode(STR_, CdBytes, Report));
                                     } 
	      break; case fmAltEKey: {
                                      ClearAllLabels();
                                      ResetReceivePtr();
                                      Report = 0;
                                     } 
	      break; case fmAltWKey: {
                                      strcpy(f_NAME, "recbuffr.obj");
                                      f = fopen(f_NAME, "w+b");
                                      _SETIO(f != NULL, FileNotFound);
                                      _SETIO(fwrite(RecBuf, MaxReceiveBuf + 1, 1, f) == 1,
                                      FileWriteError);
                                      if (f != NULL)
                                         fclose(f);
                                     }    
              break; case       '#': About();                   
	      break; case fmAltIKey: InitSerial();
	      break; case fmAltRKey: {
                                      Menu[2]->Show();
                                      ReadInFile();
                                      Menu[2]->Hide();
                                     } 
	     }
	  }
	else
	  {
	   Win[0].WriteChr(ch);
	   switch (ch)
	     {

	       //                             #13: writeLn;
	       case '\b':
	      Win[0].ClrEol();
	      break;
	     }
	   Int14Call->Send(ch);

	   ch = '~';
	  } // case
          
        doupdate();  
    } while (ch != fmAltF10Key);   // <ALT-ESC>
  ch = '\0';

  Menu[7]->Hide();
  Menu[4]->Hide();
  Win[2].Done();
  Win[0].Done();

/*
  Win[0].Init(2, 3, 62, _Display_.Height - 1, VC[0][0], VC[0][1], VC[0]
	      [2], VC[0][3], Msg[0]);
  Win[2].Init(65, 3, 78, _Display_.Height - 1, VC[2][0], VC[2][1], VC[2]
	      [2], VC[2][3], Msg[2]);
*/
  Init_W0_W2();

  Win[0].Show();
  Win[2].Show();
  Menu[0]->Show();
  Menu[4]->Show();
}


static void SetNewOpCode(void)
{
  char STR_[256];

  if (!Menu[3]->ReadStr(1, FileNmCde))
    {
     *FileNmCde = '\0';
     TrimFileString(CdePath, CdeName, CdeExt, FileNmCde, 255);
     return;
    }
  Menu[3]->Hide();
  //            Win[1].Activate;
  Win[0].WriteChr('\n');
  TrimFileString(CdePath, CdeName, CdeExt, FileNmCde, 255);
  boolean success = ReadCodes(FileNmCde);
  while (!success)
    {
     sprintf(STR_, "%s\n%s%s", Msg[6], Msg[9], FileNmCde);
     ErrorMsg(STR_);
     Menu[3]->Show();
     if (!Menu[3]->ReadStr(1, FileNmCde))
       return;
     TrimFileString(CdePath, CdeName, CdeExt, FileNmCde, 255);
     Menu[3]->Hide();
     Win[0].Clear();
     success = ReadCodes(FileNmCde);
    }
  Menu[3]->Show();
  Menu[3]->Display(1, FileNmCde, Menu[3]->AskColor);
}


static void ZwergReset(void)
{
  //
  //Port[BaseAddr+4]:=Port[BaseAddr+4] or 1;
  //

//  uchar b = PORT(BaseAddr + 4);
// p2c: /var/home/pantec/src/turbo/ucdas/ucdas.p, line 1162:
// * Note: Reference to PORT [191]
//  PORT(BaseAddr + 4) = b ^ 1;
// p2c: /var/home/pantec/src/turbo/ucdas/ucdas.p, line 1163:
// * Note: Reference to PORT [191]
// p2c: /var/home/pantec/src/turbo/ucdas/ucdas.p, line 1163:
// * Warning: Invalid assignment [168]
//  Noise();
//  PORT(BaseAddr + 4) = b;
// p2c: /var/home/pantec/src/turbo/ucdas/ucdas.p, line 1165:
// * Note: Reference to PORT [191]
// p2c: /var/home/pantec/src/turbo/ucdas/ucdas.p, line 1165:
// * Warning: Invalid assignment [168]
}


static void OptionMenu(void)
{
  char STR_[256];

  Menu[3]->Show();
  Menu[3]->Display(1, FileNmCde, Menu[3]->AskColor);
  Menu[3]->Display(2, WToString(STR_, MaxSendCount), Menu[3]->AskColor);

  do
    {
     ch = Menu[3]->GetEvent();
     switch (ch)
       {
	      case '\0': switch (Menu[3]->GetEvent())
                           {
                                   case '-': ExitProc.Halt();
                            break; case   fmF2Key: WriteFile();
                            break; case ';': Browser.Exec();
	                   }
	break; case 'O': SetNewOpCode();
	break; case 'S': InitSerial();
	break; case 'C': {
                          Menu[3]->ReadWord(2, MaxSendCount);
                	  if (MaxSendCount > MaxCodeBuffer)
                             {
                              MaxSendCount = MaxCodeBuffer & 0x0ffff;
                              Menu[3]->Display(2, WToString(STR_, MaxSendCount),
                                         	Menu[3]->AskColor);
                             }
                         }    
        break; case '#': About();                   
	break; case 'R': ZwergReset();
	 //
	 //'E': begin
	 //       ToggleEGALines;
	 //       Menu[4]^.Show;
	 //     end;
	 //
       } // case
       doupdate();
    } while (ch != '\033' && ch != fmRightButton);  // do
  Menu[3]->Hide();
  ch = '~';
}



static void LLEdit(unsigned short l_addr)
{
  char st[256];

  LabelRecType* LP = (LabelRecType *)GetLabelObj(l_addr);
  TableRecType* TP = (TableRecType *)GetLabelObj(l_addr);

  switch (LP->I)
    {
            case   Lab: strcpy(st,LP->S);
                        if (Menu[8]->ReadStr(3, st))
                           sprintf(LP->S, "%.*s", MaxLabelLength, st);

     break; case Table: strcpy(st,TP->S);
                	if (Menu[8]->ReadStr(3, st))
                           {
                      	    unsigned char b = TP->B;
                            Menu[8]->ReadByte(1, b);  // read table length
                            sprintf(TP->S, "%.*s", int(MaxLabelLength - 1), st);
                            TP->B = b;
                           }
    }
}

static void LTAdd(unsigned short l_addr)
{
  char st[256],st1[256];
  unsigned char b = 16;

  if (Menu[8]->ReadStr(3, st))
     {
      Menu[8]->ReadByte(1, b);

      if (!AddTable(l_addr, st1, b))
         ErrorMsg(Msg[13]);
     }    
}

static void LLAdd(unsigned short l_addr)
{
  char st[256],st1[256];
  unsigned char b = 16;

  if (Menu[8]->ReadStr(3, st))
     {
      Menu[8]->ReadByte(1, b);

      if (!AddLabel(l_addr, st1))
         ErrorMsg(Msg[13]);
     }   
}


static void LabelMarker(void)
{
  char STR_[256];
  char STR_1[256];

  unsigned short l_addr = 0;
  unsigned short LNr = 1;
  int i;

  i=GetLabelNum(0);
  if (i>=0)
     l_addr=i;
       
  Menu[8]->Show();
  do
    {
     ch = Menu[8]->GetEvent();

     if (ch == 'T' || ch == 'E' || ch == 'D' || ch == 'A')
         Menu[8]->ReadHex(2, l_addr);
     
#ifdef DEBUG
     fprintf(stderr,"got event([%d],0x0%x)\n",ch,l_addr);
#endif          
     
     switch (ch)
       {
              case '\0': switch (Menu[8]->GetEvent())
                           {
                        	   case fmAltXKey: ExitProc.Halt();
	                    break; case   fmF1Key: Browser.Exec();
                            break; case   fmF2Key: WriteFile();
	                   }
        break; case 'A': {
#ifdef DEBUG
                	  fprintf(stderr,"l_addr: %x\n",l_addr);
#endif        
                          if (MatchAdr(l_addr)==TRUE)
                             {
                              sprintf(STR_, "%s%s", Msg[12], Msg[21]);
                              ErrorMsg(STR_);
                              if (ch == '\015')
                                 LLEdit(l_addr);
                             } 
                           else LLAdd(l_addr); 
                         }
        break; case 'E': {
                          if (MatchAdr(l_addr)==FALSE)
                             {
                              sprintf(STR_, "%s%s", Msg[15], Msg[21]);
                              ErrorMsg(STR_);
                              if (ch == '\015')
                                 LLAdd(l_addr);
                             } 
                           else LLEdit(l_addr); 
                         }
        break; case 'T': {
                          if (MatchAdr(l_addr))
                             {
                              sprintf(STR_, "%s%s", Msg[12], Msg[21]);
                              ErrorMsg(STR_);
                              if (ch == '\015')
                                 LLEdit(l_addr);
                             } 
                           else LTAdd(l_addr); 
                         }
        break; case 'D': {
                          if (!DeleteLabel(l_addr))
                             ErrorMsg(Msg[14]);
                         }
        break; case 'N': {
                          Menu[8]->ReadWord(4, LNr);
                          LNr = LNr % HighestLabel;
                         } 
       }
     
      // chose the next Number 
      if (ch == '\015' || ch == 'E' || ch == 'D' || ch == 'A' || ch == 'T')
         LNr = (LNr+1) % HighestLabel;
     
      i=GetLabelNum(LNr);
      if (i>=0)
         l_addr=i;
         
      sprintf(STR_1, "%s%d", Msg[10], LNr);
      Menu[8]->Display(4, STR_1, Menu[8]->AskColor);

      if (!GetLabelStr(l_addr,STR_1))
         strcpy(STR_1," ?? ");
         
      Menu[8]->Display(3, STR_1, Menu[8]->AskColor);
  
      sprintf(STR_1, "0x0%x", l_addr);
      Menu[8]->Display(2, STR_1, Menu[8]->AskColor);

//      Menu[8]->Display(1, STR_1, Menu[8]->AskColor);
  
      doupdate(); 
    } while (ch != '\033' && ch != fmRightButton);
  
  Menu[8]->Hide();
  ch = '~';
}


// Local variables for EditMenu:
struct LOC_EditMenu {
  uchar i, j, y, wi, h;
} ;


void str_del(char* S,int c)
{
  char st[256];
  char* s;
  
  s = S;
  s += c;

  if (strlen(S)<c)
      strcpy(S,"\0");
    else
     {
      strcpy(st,s);
      strcpy(S,st);
     }  
}

//void UpDateEditArea(struct LOC_EditMenu *LINK)
void UpDateEditArea(int h, int y, int wi)
{
  int Pos;
  long space;
  short k;

  char st[256];
  char STR_[256];
  char STR_1[256];

  for (short j = 0; j < h ; j++)
    {
     GetStringFast(40, y + j, wi + 8, &st[0]);
     if (st[4] == 'h')
       {
#ifdef DEBUG
//	fprintf(stderr,"entry: ]%c[ %d\n",st[4],h); 
#endif        
	strcpy(STR_,st);
	sprintf(STR_1, "%.4s", STR_);
	sscanf(STR_1, "%x", &Pos);
	strdelete(&st[0], 1,5);

#ifdef DEBUG
//      fprintf(stderr,"STR_1: ]%s[  Pos: %x\n",STR_1,Pos); 
//      fprintf(stderr,"STR_ : ]%s[\n",STR_); 
//	fprintf(stderr,"st   : ]%s[\n",st); 
#endif        
	short i = 0;
	while ((i < MaxCdByte) && (strlen(st)>0))
	  {
	   while (!(
                    ((st[0] >= 'a') && (st[0] <= 'f')) ||
		    ((st[0] >= 'A') && (st[0] <= 'F')) ||
		    isdigit(st[0])
                   )  &&  (strlen(st)>0))
	     strdelete(&st[0], 1, 1);

           if (strlen(st)>=2)  
              {
  	       sprintf(STR_, "%.2s", st);
	       int c = sscanf(STR_, "%x", &k) == 0;
	       if (c == 0)
	          {
                   CdBytes[i] = k;
                   i++;
	          }
    	       strdelete(&st[0], 1,2);
              } // if
	  } // while

	for (short n=0; n<i ; n++)
   	   EditBuf[Pos + n] = CdBytes[n];

       } // if
    } // for
#ifdef DEBUG
  fprintf(stderr,"UpdateEdit(return)\n"); 
#endif        
}

static void RestoreEditArea(struct LOC_EditMenu *LINK)
{
  uchar k;
  unsigned short Pos;
  long space;
  
  char st[256];
  char STR_[256];
  char STR_1[256];
  Str3 STR_2;

  uchar FORLIM_ = LINK->h;
  for (uchar j = 0; j <= FORLIM_ - 1; j++)
    {
     GetStringFast(40, LINK->y + j, LINK->wi + 8, st);
#ifdef DEBUG
     fprintf(stderr,"st: ]%s[\n",st); 
#endif        
     if (st[4] == 'h')
       {
	sprintf(STR_1, "%.4s", st);
	sscanf(STR_1, "%x", &Pos);
	strdelete(&st[0], 1, 5);
#ifdef DEBUG
        fprintf(stderr,"pos: 0x0%x\tst: %s\t]%s[\n",Pos,STR_1,st); 
#endif        
	uchar i = 0;
	while (i < MaxCdByte && *st != '\0')
	  {
	   while (!(st[0] >= 'a' && st[0] <= 'f' ||
		    st[0] >= 'A' && st[0] <= 'F' ||
		    isdigit(st[0])) && *st != '\0')
	     strdelete(&st[0], 1, 1);

           if (strlen(st)>=2)  
              {
               sprintf(STR_, "%.2s", st);
               int c = sscanf(STR_, "%x", &k) == 0;
               if (c == 0)
                  {
                   WriteStringFast(i * 3 + 48, LINK->y + j, Win[0].NormColor,
			      HexaByte(STR_2, EditBuf[Pos + i], false));
                   i++;
                  }
	       strdelete(&st[0], 1, 2);
              } 
	  }
       }
    }
#ifdef DEBUG
  fprintf(stderr,"RestoreEdit(return)\n");  
#endif
}

static void CaseChar(struct LOC_EditMenu *LINK)
{
#ifdef DEBUG
  fprintf(stderr,"CaseChar(entry)\n");  
#endif

  switch (ch)
    {

      case fmUpKey:
     if (LINK->j == 0)
       LINK->j = LINK->h - 1;
     else
       LINK->j--;
     break;

      case fmDownKey:
     LINK->j = (LINK->j + 1) % LINK->h;
     break;

      case fmLeftKey:
     if (LINK->i == 0)
       {
	if (LINK->j == 0)
	  LINK->j = LINK->h - 1;
	else
	  LINK->j--;
	LINK->i = LINK->wi - 1;
       }
     else
       LINK->i--;
     break;

      case fmRightKey:
     LINK->i = (LINK->i + 1) % LINK->wi;
     if (LINK->i == 0)
       LINK->j = (LINK->j + 1) % LINK->h;
     break;
    }
#ifdef DEBUG
  fprintf(stderr,"CaseChar(return)\n");  
#endif
}



static void EditMenu(void)
{
  struct LOC_EditMenu V;
  uchar x = 48;
  boolean UpDated = TRUE;
  char STR_[256];

  V.i = 0;
  V.j = 0;

  //40
  V.y = 2;

  V.wi = Win[0].Coord.x2 - x;
  V.h = Win[0].Coord.y2 - Win[0].Coord.y1 + 1;

  SetCursAbs(x + V.i, V.y + V.j);
  BigCursor();
  do
    {
     ch = Menu[0]->GetEvent();
     switch (ch)
       {
	 case '\0': ch=Menu[0]->GetEvent();
                    switch (ch)
	              {
                              case     '-': ExitProc.Halt();
//                       break; case fmF2Key: WriteFile();
                       break; case     ';': {
                                             Browser.Exec();   
                                             BigCursor();
                                            } 
                       break; case     '<': {
                                             UpDateEditArea(V.h,V.y,V.wi); 
                                             UpDated = TRUE;
                                            } 
	              }

                    CaseChar(&V);
                    while (GetCharAbs(x + V.i, V.y + V.j) == ' ' &&
                          (ch == 'M' || ch == 'K' || ch == 'P' || ch == 'H'))
                       CaseChar(&V);

	break; default:
	if (isdigit(ch) || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f')
	  {
	   if (ch >= 'a' && ch <= 'f')
	     ch = toupper(ch);

	   char TEMP_ = GetCharAbs(x + V.i, V.y + V.j);
	   if (TEMP_ >= 'a' && TEMP_ <= 'f' || TEMP_ >= 'A' && TEMP_ <= 'F' ||
	       isdigit(TEMP_))
	     WriteCharAbs(x + V.i, V.y + V.j, EditAttr, ch);

	   ch = fmRightKey;
	   do
	     {
	      CaseChar(&V);
	     } while (GetCharAbs(x + V.i, V.y + V.j) == ' ');

	   UpDated = FALSE;
	  }
	break;
       } // case

      SetCursAbs(x + V.i, V.y + V.j);
      
/*
      touchwin(Win[0].w_save);
      touchwin(Win[0].w_self);
      touchwin(Win[0].w_in);
*/      
      doupdate(); 
    } while (ch != '\033' && ch != fmRightButton);

  if (!UpDated)
    {
     if (ch == '\033')
       {
	sprintf(STR_, "%s%s", Msg[20], Msg[21]);
	ErrorMsg(STR_);
       }
     if (ch == '\015' || ch == fmRightButton)
       UpDateEditArea(V.h,V.y,V.wi);
     else
       RestoreEditArea(&V);
    }
  ch = '~';

/*
#ifdef DEBUG
  fprintf(stderr,"befor touch\n"); 
#endif        
  touchwin(Win[0].w_save);
  touchwin(Win[0].w_self);
  touchwin(Win[0].w_in);
#ifdef DEBUG
  fprintf(stderr,"before change\n"); 
#endif        
*/  
  ChangeAttr(x, V.y + 1, x + V.wi, V.y + V.h, Win[0].NormColor);
#ifdef DEBUG
//  fprintf(stderr,"before nocursor\n"); 
#endif        
  NoCursor();

#ifdef DEBUG
  fprintf(stderr,"return EditMenu\n"); 
#endif        
}


static void Abort(char *s)
{
  puts(s);
  ExitProc.Halt();
}



main(int argc, char *argv[])
{
  char STR_[256];
  ExitProcType SaveExit;


  PASCAL_MAIN(argc, argv);
  Browser.F = NULL;
  Datei = NULL;

  SaveExit.proc = ExitProc.proc;
  SaveExit.link = ExitProc.link;
  
  ExitProc.proc = UCDAsExit;  
  ExitProc.link = 0;

  EnterProgSelfPath("ucdas");

#ifdef DEBUG
  WriteConfig();
  fprintf(stderr,"passed point 2\n");
#endif
    
  ReadConfig();
  InstallTMenuExit();
   
  Int14Call = new Int14Type;
  Int14Call->Init(RS232Nr - 1, 7, 8, 2, 1);
  if (Int14Call->LastError)
    Abort("no COM-Ports available ..");


//  PORT(BaseAddr + 1) = 0;
// p2c: /var/home/pantec/src/turbo/ucdas/ucdas.p, line 1571:
// * Note: Reference to PORT [191]
// p2c: /var/home/pantec/src/turbo/ucdas/ucdas.p, line 1571:
// * Warning: Invalid assignment [168]
//  PORT(BaseAddr + 4) = 10;
// p2c: /var/home/pantec/src/turbo/ucdas/ucdas.p, line 1572:
// * Note: Reference to PORT [191]
// p2c: /var/home/pantec/src/turbo/ucdas/ucdas.p, line 1572:
// * Warning: Invalid assignment [168]

  EditBuf = new uchar[MaxCodeBuffer + MaxCdByte + 1];
  RecBuf = new uchar[MaxReceiveBuf + MaxCdByte + 1];

  ch = '\0';

  SetBaud(Standard.Baud);
  SetLocal(Standard.Data, Standard.Parity, Standard.Stop);
  ReInitCRT();
  InstallOpcodeExit();
  Win[0].WriteStr(Msg[17]);


  if (!ReadCodes(FileNmCde))
    {
     sprintf(STR_, "%s\n%s%s", Msg[6], Msg[9], FileNmCde);
     ErrorMsg(STR_);
     Menu[3]->Show();
     SetNewOpCode();
     Menu[3]->Hide();
     Win[0].Activate();
    }


  Win[0].WriteStr(Msg[18]);

  ch = Menu[0]->GetEvent();
  Win[0].Clear();

 
  if ((strlen(LastObject) == 0) || (FileExists(LastObject) == FALSE))
    sprintf(LastObject,"%s%s%s",LIBPATH,CdeName,ObjExt);

  if (Datei != NULL)
    Datei = freopen(LastObject, "r+b", Datei);
  else
    Datei = fopen(LastObject, "r+b");

  _SETIO(Datei != NULL, FileNotFound);
  if (P_ioresult == 0)
    {
     MaxRead = fread(EditBuf, 1, MaxCodeBuffer, Datei);
     if (Datei != NULL)
       fclose(Datei);
     Datei = NULL;
    }

  strcpy(SymName, ObjName);
  *FileNmSym = '\0';

  TrimFileString(BinLoadPath, SymName, SymExt, FileNmSym, 255);

#ifdef DEBUG
  fprintf(stderr,"LoadSymbols(%s)\n",FileNmSym); 
#endif 

  if (FileExists(FileNmSym) == TRUE)
     ReadSymbols(FileNmSym);

  Index = 0;
  SendCount = 0;
  WriteCount = 0;

  do
    {
     UpdateStatus();
     ch = Menu[0]->GetEvent();
     switch (ch)
	  {
	    case '\0': {
                        ch = Menu[0]->GetEvent();
                        switch (ch)
                          {
	                          case    '-': ExitProc.Halt();
                           break; case    'P': Win[0].ScrollUp(nextCodeLine(STR_));
                           break; case '\022': SeekPos();
                           break; case    ';': Browser.Exec();
                           break; case '\037':
                              	  case    '<': WriteFile();
                           break; case '\027': InitSerial();
                           break; case    '&': {
                                        	Menu[2]->Show();
                                                NewFileName();
                                                Menu[2]->Hide();
                                                ch = '~';
                                               } 
                           break; case    ' ': {
                                        	Menu[2]->Show();
                                                SendBuffer();
                                                Menu[2]->Hide();
                                                ch = '~';
                                               } 
                           break; case '\030': {
                                        	Menu[3]->Show();
                                                SetNewOpCode();
                                                Menu[3]->Hide();
                                                ch = '~';
                                               } 
                           break; case    '=': {
                                        	Menu[2]->Show();
                                                NewFileName();
                                                Menu[2]->Hide();
                                                ch = '~';
                                               } 
                          } // case                     
	               }
	   break; case        '\015':
	          case     wsDownKey:
	          case fmRightButton: Win[0].ScrollUp(nextCodeLine(STR_));
	   break; case           '#': About();
	   break; case           'F': FileMenu();
	   break; case           'O': OptionMenu();
	   break; case           'E': EditMenu();
	   break; case           'P': PassOne(false);
	   break; case           'T': Monitoring();
	   break; case           'L': LabelMarker();
	   break; case           'A': ExitProc.Halt();
	   break; case           '-': ch = '~';
	   break; case           'C': {
                                       Win[0].Clear();
                                       ClearAllLabels();
                                       TextLines = 0;
                                       WriteCount = 0;
                                       SendCount = 0;
                                       Index = 0;
                                      }  
	  } // case


#ifdef DEBUG
  fprintf(stderr,"before doupdate\n"); 
#endif                
      doupdate();  

#ifdef DEBUG
  fprintf(stderr,"after doupdate\n"); 
#endif            
  } while ( 1 == 1);
}






// End.
