I have a few questions regarding the sending of commands in C, and the solutions are probably obvious once I see them. I should mention that if I enter the exact string of characters followed by the enter key, the corresponding command DOES WORK as it should.
Code:
void ReadSock()
{ // main accept() loop
rc = read(new_fd, recv_client_msg +bytesread, sizeof(recv_client_msg) - bytesread);
if(read_pin(0x21, 12)==1) {
if(rc>0)
{
if(write(new_fd,recv_client_msg +bytesread , rc) <0 ) { printf("error writing to stream socket\n"); }
bytesread +=rc;
for(b = 0; b < bytesread; ++b)
{
if (recv_client_msg[b]=='\n'|| recv_client_msg[b] == '\r')
{
recv_client_msg[b+1]=0;
printf("%s\n %d\n" , recv_client_msg, b);
if(b>0 && memcmp(recv_client_msg, "s1p1" , b)==0)
{
commandSwitch(1,1);
}
if(b>0 && memcmp(recv_client_msg, "s1p2" , b)==0)
{
commandSwitch(1,2);
}
if(b>0 && memcmp(recv_client_msg, "s2p1" , b)==0)
{
commandSwitch(2,1);
}
if(b>0 && memcmp(recv_client_msg, "s2p2" , b)==0)
{
commandSwitch(2,2);
}
if(b>0 && memcmp(recv_client_msg, "x" , b)==0)
{
close(new_fd); new_fd=-1;
}
bytesread=0;
}
}
}//end of if rc==0
}//end off if wrire pin
else{
if (send(new_fd, "Unable to operate remotely, system is in local mode", 51, 0) == -1);
}
if(rc<=0)
{
if(rc==0){printf("connection closed nicely at the other end\n"); close(new_fd); new_fd=-1;}
else{if(errno!=EAGAIN) {fprintf(stderr,"Error reading: %s\n", strerror(errno));}}
}
}
1). How could i make it so after I type in a command and it executes for my remote terminal to advance to the next line automatically? As, it stands after processing the command the cursor resets to the start of the current line upon pressing enter and if I press down on the keyboard it registers as a character so I have to then press enter again.
2). Pretty small bug but however a problem: If I press enter if I have happened to only press the "s" key, all 4 commands execute as they all begin with the character "s". Similarly if I only type "s1" before pressing enter, the two commands beginning with "s1" execute. Long story short I would only like my system to execute a command if the characters are entered exactly as they are.
3). And Finally: How do I make it so pressing the backspace key doesn't register as a character? e.g. if i want to type "s2p1" but if I type "s2p2" by accident then press backspace followed by 1, even though I will see "s2p1" on my terminal, it won't run the command as I have in actual fact entered 6 characters: s,2,p,2,backspace,1.
Hope these can be answered, I'm sure the changes required a quite small and trivial, especially with 2).