//----------------------------------------------------------------- // netchat.cpp // // This Linux application implements PC-to-PC communication // via the '/dev/nic' device-file interface to functions in // our 'nicpoll.c' character-mode device-driver for Intel's // Pro1000 network controller on 'anchor' cluster stations. // (You must link this program with the 'ncurses' library.) // // compile using: $ gcc netchat.cpp -lncurses -o netchat // // programmer: ALLAN CRUSE // written on: 10 MAR 2008 //----------------------------------------------------------------- #include // needed for initscr(), refresh(), etc #include // needed for STDIN_FILENO #include // needed for open(), O_RDWR #define KEY_LO 32 #define KEY_HI 126 #define KEY_LF 10 #define KEY_CR 13 #define KEY_ESC 27 #define KEY_TAB 9 #define KEY_RUB 127 #define KEY_BLANK 32 int aux, kbd = STDIN_FILENO; // file-descriptor numbers char device_name[] = "/dev/nic"; // name of the device file void writeto_window( WINDOW *win, int datum ) { switch ( datum ) { case KEY_LF: case KEY_CR: waddch( win, KEY_LF ); waddch( win, KEY_CR ); break; case KEY_TAB: waddch( win, KEY_TAB ); case KEY_ESC: break; case KEY_RUB: int row, col; getyx( win, row, col ); if ( col > 0 ) --col; wmove( win, row, col ); waddch( win, KEY_BLANK ); wmove( win, row, col ); break; default: if (( datum < KEY_LO )||( datum > KEY_HI )) break; else waddch( win, datum ); break; } wrefresh( win ); } WINDOW *subwindow( int rows, int cols, int top, int left ) { WINDOW *win = subwin( stdscr, rows, cols, top, left ); box( win, ACS_VLINE, ACS_HLINE ); wrefresh( win ); delwin( win ); win = subwin( stdscr, rows-2, cols-2, top+1, left+1 ); scrollok( win, TRUE ); leaveok( win, FALSE ); return win; } int main( void ) { if ( ( aux = open( device_name, O_RDWR ) ) < 0 ) { printf( "cannot open \'%s\' \n", device_name ); return -1; } initscr(); // creates windows 'stdscr' and 'curscr' WINDOW *rx_win = subwindow( 12, 80, 0, 0 ); WINDOW *tx_win = subwindow( 12, 80, 12, 0 ); raw(); // puts console keyboard into 'raw' mode noecho(); // and turns off echoing of console input move( 13, 1 ); // places cursor at top corner of 'tx_win' refresh(); // updates actual screen to match 'stdscr' fd_set permset; FD_ZERO( &permset ); // initialize set to zeros FD_SET( kbd, &permset ); // listen to console input FD_SET( aux, &permset ); // listen to remote device int ndev = 1 + aux; // maximum fileno for poll while ( 1 ) { int row, col, inch = 0; fd_set readset = permset; if ( select( ndev, &readset, NULL, NULL, NULL ) < 0 ) break; if ( FD_ISSET( kbd, &readset ) ) if ( read( kbd, &inch, 1 ) > 0 ) { writeto_window( tx_win, inch ); write( aux, &inch, 1 ); if ( inch == KEY_ESC ) break; } if ( FD_ISSET( aux, &readset ) ) if ( read( aux, &inch, 1 ) > 0 ) writeto_window( rx_win, inch ); getyx( tx_win, row, col ); move( row+13, col+1 ); refresh(); } endwin(); }