#ifndef __EDWIN_UTILITY_H__
#define __EDWIN_UTILITY_H__ 1
#include <perms.h>
//int Instream(void);
//int Outstream(void);
extern int Uinfi(int Parm);
extern void Console(int Fn, int *A, int *L);
_imp_string Buff = _imp_str_literal("");
int Viewing = 0;
void Ttmode(int State) {
  extern int Uinfi(int I);
  extern void Messages(_imp_string State);
  static const int Messageinfo = 22;
  static int Messagestate = 0;
  int A;
  int L;
  static unsigned char Setgaf[5 /*0:4*/] = {2, 11, 1, 17, 1};
  static unsigned char Setnog[5 /*0:4*/] = {2, 11, [2 ... 4] = 0};
  if (Uinfi(2) == 2) {
    Selectoutput(0);
    Printstring(_imp_str_literal("Graphics I/O in batch mode ignored"));
    Newline();
    Viewing = -1;
    return;
  }
  if (!State) {
    A = Addr(Setnog[0]);
    L = 1;
    Console(17, &A, &L);
    if (!Messagestate) Messages(_imp_str_literal("ON"));
    return;
  }
  if (State & 1) {
    if (Device == 2 || Device == 3) Setgaf[0] = 4;
    A = Addr(Setgaf[0]);
    L = 1;
    Console(17, &A, &L);
    Messagestate = Uinfi(Messageinfo);
    if (!Messagestate) Messages(_imp_str_literal("OFF"));
    return;
  }
}
void Killinput(void) {
  int A;
  int L;
  Console(8, &A, &L);
}
void Flushoutput(void) {
  int A;
  int L;
  if (/*Buff == _imp_str_literal("")*/Buff.length == 0 || Viewing < 0) return;
  if (!Viewing) {
    A = Addr(Buff) + 1;
    L = *Length(Buff);
    Console(10, &A, &L);
  } else {
    L = Outstream();
    Selectoutput(Viewing);
    for (A = 1; A <= *Length(Buff); A++) Printch(*Charno(Buff, A));
    Selectoutput(L);
  }
  Buff = _imp_str_literal("");
}
void Ttput(int I) {
  Buff = _imp_join(Buff, Tostring(I));
  if (*Length(Buff) > 253) Flushoutput();
}
int Ttread(void) {
  int I;
  int S;
  S = Instream();
  Selectinput(0);
  Readch(&I);
  Selectinput(S);
  return (I);
}
int Ttget(void) { return (Ttread()); }
int EdwinMuldiv(int A, int B, int C) {
  long long int Ab;
  long long int Res;
  long long int Rem;
  if (C) {
    Ab = Lengtheni(A) * Lengtheni(B);
    Res = Ab / Lengtheni(C);
    Rem = Ab - Res * Lengtheni(C);
    if (Rem > C / 2) Res++;
  } else
    Res = 0;
  return (Res);
}
#endif
