int input_init() { if( is_init ) return 1; is_init = 1; input_common_init( &interrupt_handler ); if( setupterm( 0, STDOUT_FILENO, 0) == ERR ) { debug( 0, _( L"Could not set up terminal" ) ); exit(1); } output_set_term( env_get( L"TERM" ) ); input_terminfo_init(); /* If we have no keybindings, add a few simple defaults */ if( !al_get_count( &mappings ) ) { input_mapping_add( L"", L"self-insert" ); input_mapping_add( L"\n", L"execute" ); input_mapping_add( L"\t", L"complete" ); input_mapping_add( L"\x3", L"commandline \"\"" ); input_mapping_add( L"\x4", L"exit" ); input_mapping_add( L"\x5", L"bind" ); } return 1; }
/** Initialize various subsystems. This also closes stdin and replaces it with a copy of stderr, so the reading of completion strings must be done before init is called. */ static void init( int mangle_descriptors, int out ) { struct sigaction act; static struct termios pager_modes; char *term; if( mangle_descriptors ) { /* Make fd 1 output to screen, and use some other fd for writing the resulting output back to the caller */ int in; out = dup( 1 ); close(1); close(0); /* OK to not use CLO_EXEC here because fish_pager is single threaded */ if( (in = open( ttyname(2), O_RDWR )) != -1 ) { if( dup2( 2, 1 ) == -1 ) { debug( 0, _(L"Could not set up output file descriptors for pager") ); exit( 1 ); } if( dup2( in, 0 ) == -1 ) { debug( 0, _(L"Could not set up input file descriptors for pager") ); exit( 1 ); } } else { debug( 0, _(L"Could not open tty for pager") ); exit( 1 ); } } if( !(out_file = fdopen( out, "w" )) ) { debug( 0, _(L"Could not initialize result pipe" ) ); exit( 1 ); } env_universal_init( 0, 0, 0, 0); input_common_init( &interrupt_handler ); output_set_writer( &pager_buffered_writer ); sigemptyset( & act.sa_mask ); act.sa_flags=0; act.sa_handler=SIG_DFL; act.sa_flags = 0; act.sa_handler= &handle_winch; if( sigaction( SIGWINCH, &act, 0 ) ) { wperror( L"sigaction" ); exit(1); } handle_winch( 0 ); /* Set handler for window change events */ tcgetattr(0,&pager_modes); /* get the current terminal modes */ memcpy( &saved_modes, &pager_modes, sizeof(saved_modes)); /* save a copy so we can reset the terminal later */ pager_modes.c_lflag &= ~ICANON; /* turn off canonical mode */ pager_modes.c_lflag &= ~ECHO; /* turn off echo mode */ pager_modes.c_cc[VMIN]=1; pager_modes.c_cc[VTIME]=0; /* */ if( tcsetattr(0,TCSANOW,&pager_modes)) /* set the new modes */ { wperror(L"tcsetattr"); exit(1); } if( setupterm( 0, STDOUT_FILENO, 0) == ERR ) { debug( 0, _(L"Could not set up terminal") ); exit(1); } term = getenv("TERM"); if( term ) { wchar_t *wterm = str2wcs(term); output_set_term( wterm ); free( wterm ); } /* Infer term256 support */ char *fish_term256 = getenv("fish_term256"); bool support_term256; if (fish_term256) { support_term256 = from_string<bool>(fish_term256); } else { support_term256 = term && strstr(term, "256color"); } output_set_supports_term256(support_term256); }
int main( int argc, char **argv) { setlocale( LC_ALL, "" ); if( argc == 2 ) { static char term_buffer[2048]; char *termtype = getenv ("TERM"); char *tbuff = malloc( sizeof(char)*9999); char *res; tgetent( term_buffer, termtype ); res = tgetstr( argv[1], &tbuff ); if( res != 0 ) { while( *res != 0 ) { printf("%d ", *res ); res++; } printf( "\n" ); } else { printf("Undefined sequence\n"); } } else { char scratch[1024]; unsigned int c; struct termios modes, /* so we can change the modes */ savemodes; /* so we can reset the modes when we're done */ input_common_init(0); tcgetattr(0,&modes); /* get the current terminal modes */ savemodes = modes; /* save a copy so we can reset them */ modes.c_lflag &= ~ICANON; /* turn off canonical mode */ modes.c_lflag &= ~ECHO; /* turn off echo mode */ modes.c_cc[VMIN]=1; modes.c_cc[VTIME]=0; tcsetattr(0,TCSANOW,&modes); /* set the new modes */ while(1) { if( (c=input_common_readch(0)) == EOF ) break; if( (c > 31) && (c != 127) ) sprintf( scratch, "dec: %d hex: %x char: %c\n", c, c, c ); else sprintf( scratch, "dec: %d hex: %x\n", c, c ); writestr( scratch ); } /* reset the terminal to the saved mode */ tcsetattr(0,TCSANOW,&savemodes); input_common_destroy(); } return 0; }