/*
* Read from a file. Can return:
* - zero if the read was completely successful
* - the number of bytes _not_ read, if the read was partially successful
* - the number of bytes not read, plus the top bit set (0x80000000), if
* the read was partially successful due to end of file
* - -1 if some error other than EOF occurred
* This function receives a character from the UART, processes the character
* if required (backspace) and then echo the character to the Terminal
* Emulator, printing the correct sequence after successive keystrokes.
*/
int _sys_read(FILEHANDLE fh, unsigned char * buf,
unsigned len, int mode)
{
int pos=0;
do {
buf[pos]=UART_read();
// Advance position in buffer
pos++;
// Handle backspace
if(buf[pos-1] == '\b')
{
// More than 1 char in buffer
if(pos>1)
{
// Delete character on terminal
UART_write('\b');
UART_write(' ');
UART_write('\b');
// Update position in buffer
pos-=2;
}
else if (pos>0) pos--; // Backspace pressed, empty buffer
}
else UART_write(buf[pos-1]); // Echo normal char to terminal
}while(buf[pos-1] != '\r');
buf[pos]= '\0'; // Ensure Null termination
return 0;
}
|