#include	"trust.h"
#include	"shm.h"
#include	"repeater.h"

int KeyIn(void)
{
        char    buf[128];

        read (cmdin, buf, sizeof(buf));

        DoKeyInCommand(buf);

        return(0);
}

int DoKeyInCommand(char *str)
{
        int     k, n;
	struct	shm_form	send_msg;
	struct	sockaddr_in	db_out;
	int	ret;

        k = strlen(str);
        for (n = 0 ; n < k ; n++)
        {
                if (str[n] == 0x0a) str[n] = 0x00;
                if (str[n] == 0x0d) str[n] = 0x00;
        }

        if (strlen(str))
        {
                if((strncmp(str,"end", strlen(str)) == 0) 
			|| strncmp(str, "exit", strlen(str)) == 0){
                        killpg(getpid(),SIGTERM);
                        CloseSocket();
                        return(1);
                }
		else if (strncmp(str, "stop", 4) == 0)
		{
			if (strlen(str) > 12)
			{
               			memcpy (send_msg.dstRep, str+5, 8);
               			send_msg.type = ReqEnd;
               			db_out.sin_family = AF_INET;
            	   		db_out.sin_port = dbInPort;
              			db_out.sin_addr = dbIP;
               			sendto (TrustDB_in, &send_msg, 
					sizeof (struct shm_form), 0,
                       	    	    	(struct sockaddr *)&db_out, 
					sizeof(db_out));
			}
		}
		else if (strncmp (str, "table", 5) == 0)
		{
			for (n = 0 ; n < MAX_SHM ; n++)
			{
				if (shm_msg->shmem[n].mem.type)
				{
					printf ("%2.2x %8.8s %8.8s %8.8s %8.8s\n",
						shm_msg->shmem[n].mem.type,
               				 	shm_msg->shmem[n].mem.srcCall,
                				shm_msg->shmem[n].mem.dstCall,	
						shm_msg->shmem[n].mem.Rep2Call,
						shm_msg->shmem[n].mem.Rep1Call);
				}
			}
		}
        }

        return(0);
}

