Files
ttyfilter/localecho.c

73 lines
1.7 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <termio.h>
#include <string.h>
/*
*------------------------------------------------------------------------------
* Definitions
*------------------------------------------------------------------------------
*/
#define TERMINATOR '\e'
void terminit( struct termios* ptopt );
void termrst( struct termios* ptopt );
/*
*------------------------------------------------------------------------------
* MAIN - localecho - trivial raw echo stdin to stdout
*------------------------------------------------------------------------------
*/
int main( int argc, char* argv[] ) {
unsigned char bHex = 0;
struct termios sTermOpt;
char c;
/* use --hex to return hex codes instead of character */
bHex = ( argc > 1 ) && ( strcmp(argv[1], "--hex") == 0 );
terminit( &sTermOpt );
while ( c != TERMINATOR ) {
c = getchar();
if ( bHex ) printf("%02X ", c);
else putc( c, stdout );
}
putc( '\n', stdout );
termrst( &sTermOpt );
return 0;
}
/*
*------------------------------------------------------------------------------
* Local Functions
*------------------------------------------------------------------------------
*/
/* Init platform terminal options to remove buffering and local echo -- make it raw! */
void terminit( struct termios* ptopt ) {
static struct termios newt;
tcgetattr( STDIN_FILENO, ptopt);
newt = *ptopt;
/* clear ICANON (buffering) and ECHO */
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr( STDIN_FILENO, TCSANOW, &newt);
}
/* Reset terminal as it was before invocating this tool */
void termrst( struct termios* ptopt ) {
tcsetattr( STDIN_FILENO, TCSANOW, ptopt);
}