/* adprakt.h
 * Headerdatei mit Spezialfunktionen fuer das
 * Praktikum Algorithmen & Datenstrukturen
 */

#ifndef __ADPRAKT__
#define __ADPRAKT__

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <termios.h>
#include <sys/types.h>

int echo_on()
  /* Echo input on screen */
  {
  struct termios attributes;

  if(tcgetattr(STDIN_FILENO,&attributes) != 0)
    return (-1);
  attributes.c_lflag |= ECHO;
  if(tcsetattr(STDIN_FILENO,TCSANOW,&attributes) != 0)
    return (-1);
  return(0);
  }

int echo_off()
  /* Don't echo input on screen */
  {
  struct termios attributes;

  if(tcgetattr(STDIN_FILENO,&attributes) != 0)
    return (-1);
  attributes.c_lflag &= ~(ECHO);
  if(tcsetattr(STDIN_FILENO,TCSAFLUSH,&attributes) != 0)
    return (-1);
  return(0);
  }

int set_waitkey()
  /*Wait forever for the keyboard to be touched */
  {
  struct termios attributes;

  if(tcgetattr(STDIN_FILENO,&attributes) != 0)
    return (-1);
  attributes.c_lflag    &= ~(ICANON);
  attributes.c_cc[VMIN]  = 1;
  attributes.c_cc[VTIME] = 1;
  if(tcsetattr(STDIN_FILENO,TCSANOW,&attributes) != 0)
    return (-1);
  return(0);
  }

int getch()
  {
  int istat, key;
  char buf[2];

  if (0 > echo_off()) return EOF;
  if (0 > set_waitkey()) return EOF;
  istat = read(STDIN_FILENO,&buf,1);
  if (istat < 0) key = EOF;
  else           key = (int) buf[0];
  echo_on();
  return key;
  }

int relais(int R1, int R2, int R3, int R4, int R5, int R6, int R7, int R8, char Result[200])
  {
  /* Switch relaisbox at blasckhole.ee.fhm.edu according to R1 .. R8              */
  /* Return value: -1 if an error occured else return value of relaisbox-Software */
  /* Result: result-string of relaisbos-software                                  */
  char command[200] = "/usr/local/bin/relais-client.pl";
  char filename[100];
  int erg, pid;
  FILE* tmp;
  pid = getpid();
  sprintf(filename,"/tmp/adprakt.tmp.%d",pid);
  if (R1 != 0) strcat(command," -s1"); else strcat(command," -r1");
  if (R2 != 0) strcat(command," -s2"); else strcat(command," -r2");
  if (R3 != 0) strcat(command," -s3"); else strcat(command," -r3");
  if (R4 != 0) strcat(command," -s4"); else strcat(command," -r4");
  if (R5 != 0) strcat(command," -s5"); else strcat(command," -r5");
  if (R6 != 0) strcat(command," -s6"); else strcat(command," -r6");
  if (R7 != 0) strcat(command," -s7"); else strcat(command," -r7");
  if (R8 != 0) strcat(command," -s8"); else strcat(command," -r8");
  strcat(command," >");
  strcat(command,filename);
  erg = system(command);
  if (erg%256 != 0) return(-1);
  erg = erg/256;
  tmp = fopen(filename,"r");
  fgets(Result,199,tmp);
  fclose(tmp);
  unlink(filename);
  return(erg);
  }

int relais_off(char Result[200])
  {
  /* Switch OFF Relaisbox at blasckhole.ee.fhm.edu                                */
  /* Return value: -1 if an error occured else return value of relaisbox-Software */
  /* Result: result-string of relaisbos-software                                  */
  char command[200] = "/usr/local/bin/relais-client.pl";
  char filename[100];
  int erg, pid;
  FILE* tmp;
  pid = getpid();
  sprintf(filename,"/tmp/adprakt.tmp.%d",pid);
  strcat(command," -off >");
  strcat(command,filename);
  erg = system(command);
  if (erg%256 != 0) return(-1);
  erg = erg/256;
  tmp = fopen(filename,"r");
  fgets(Result,199,tmp);
  fclose(tmp);
  unlink(filename);
  return(erg);
  }

#endif 