Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /*
- * File: main.c
- * Author: kellyst.onge
- *
- * Created on May 16, 2019, 2:45 PM
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <termios.h>
- #include <string.h>
- #define ECH 6385181892
- #define ECHOFF 210644657169
- #define ICRNLL 210715757085
- #define ICRNLLOFF 6951278441226
- #define ONLCRON 210723261027
- #define ONLCROFF 6951285945168
- #define ECHOEON 210711002537
- #define ECHOEOFF 6951273686678
- #define OLCUCON 210723179931
- #define OLCUCOFF 6951285864072
- #define CRTSCTSON 229462300234315
- #define CRTSCTSOFF 7569935063263864
- #define ICANONON 6953619373085
- #define ICANOFF 229392187949738
- #define ERASECHAR 210711534101
- #define KTN 6385404177 //kill
- #define INTRC 6385338018
- #define STARTT 210728208851
- #define STOPP 6385703755
- #define MIN 193499273
- #define TIME 6385727636
- #define oops(s,x) { perror(s); exit(x);}
- int process_command(char *argv[]);
- void showbaud(int thespeed);
- void show_some_flags( struct termios *ttyp);
- void show_flagset();
- void showchar( );
- char getchars(char d);
- void usage(void);
- const unsigned long hash(const char *str) {
- unsigned long hash = 5381;
- int c;
- while ((c = *str++))
- hash = ((hash << 5) + hash) + c;
- return hash;
- }
- int main(int argc, char *argv[]) {
- struct termios ttyinfo;
- int i =1;
- while (argv[i] != NULL){
- char *p_command = argv[i];
- struct termios ttyinfo;
- if (tcgetattr (0, &ttyinfo) == -1){
- perror("Cannot get perams about stdin");
- exit(1);
- }
- // printf("switch\n");
- switch(hash(p_command)){
- struct termios ttyinfo;
- case ECH:
- tcgetattr (0, &ttyinfo);
- ttyinfo.c_lflag |= ECHO ;
- tcsetattr(0,TCSANOW, &ttyinfo);
- break;
- case ECHOFF:
- tcgetattr (0, &ttyinfo);
- ttyinfo.c_lflag &= ~ECHO ;
- tcsetattr(0,TCSANOW, &ttyinfo);
- break;
- case ECHOEON:
- tcgetattr (0, &ttyinfo);
- ttyinfo.c_lflag |= ECHOE ;
- tcsetattr(0,TCSANOW, &ttyinfo);
- break;
- case ECHOEOFF:
- tcgetattr (0, &ttyinfo);
- ttyinfo.c_lflag &= ~ECHOE ;
- tcsetattr(0,TCSANOW, &ttyinfo);
- break;
- case ICRNLL:
- tcgetattr (0, &ttyinfo);
- ttyinfo.c_iflag |= ICRNL ;
- tcsetattr(0,TCSANOW, &ttyinfo);
- break;
- case ICRNLLOFF:
- tcgetattr (0, &ttyinfo);
- ttyinfo.c_iflag &= ~ICRNL ;
- tcsetattr(0,TCSANOW, &ttyinfo);
- break;
- case ONLCRON:
- tcgetattr (0, &ttyinfo);
- ttyinfo.c_oflag |= ONLCR ;
- tcsetattr(0,TCSANOW, &ttyinfo);
- break;
- case ONLCROFF:
- tcgetattr (0, &ttyinfo);
- ttyinfo.c_oflag &= ~ONLCR ;
- tcsetattr(0,TCSANOW, &ttyinfo);
- break;
- case CRTSCTSON:
- tcgetattr (0, &ttyinfo);
- ttyinfo.c_lflag |= CRTSCTS ;
- tcsetattr(0,TCSANOW, &ttyinfo);
- break;
- case CRTSCTSOFF:
- tcgetattr (0, &ttyinfo);
- ttyinfo.c_lflag &= ~CRTSCTS ;
- tcsetattr(0,TCSANOW, &ttyinfo);
- break;
- case OLCUCON:
- tcgetattr (0, &ttyinfo);
- // ttyinfo.c_oflag |= OLCUC ;
- tcsetattr(0,TCSANOW, &ttyinfo);
- break;
- case OLCUCOFF:
- tcgetattr (0, &ttyinfo);
- // ttyinfo.c_oflag &= ~OLCUC ;
- tcsetattr(0,TCSANOW, &ttyinfo);
- break;
- case ICANONON:
- tcgetattr (0, &ttyinfo);
- ttyinfo.c_lflag |= ICANON ;
- tcsetattr(0,TCSANOW, &ttyinfo);
- break;
- case ICANOFF:
- tcgetattr (0, &ttyinfo);
- ttyinfo.c_lflag &= ~ICANON ;
- tcsetattr(0,TCSANOW, &ttyinfo);
- break;
- case ERASECHAR:
- tcgetattr (0, &ttyinfo);
- char* str = argv[i+1];
- char d = str[0];
- ttyinfo.c_cc[VERASE] = d ;
- tcsetattr(0,TCSANOW, &ttyinfo);
- ++argv;
- break;
- case KTN:
- tcgetattr (0, &ttyinfo);
- char* strk = argv[i+1];
- char k = strk[0];
- ttyinfo.c_cc[VKILL] = k ;
- tcsetattr(0,TCSANOW, &ttyinfo);
- ++argv;
- break;
- case INTRC:
- tcgetattr (0, &ttyinfo);
- char* strc = argv[i+1];
- char i = strc[0];
- ttyinfo.c_cc[VINTR] = i ;
- tcsetattr(0,TCSANOW, &ttyinfo);
- ++argv;
- break;
- case STARTT:
- tcgetattr (0, &ttyinfo);
- char* strs = argv[i+2];
- char x = strs[0];
- ttyinfo.c_cc[VSTART] = x ;
- tcsetattr(0,TCSANOW, &ttyinfo);
- ++argv;
- break;
- case STOPP:
- tcgetattr (0, &ttyinfo);
- char* sto = argv[i+2];
- char y = sto[0];
- ttyinfo.c_cc[VSTOP] = y ;
- tcsetattr(0,TCSANOW, &ttyinfo);
- ++argv;
- break;
- case MIN:
- tcgetattr (0, &ttyinfo);
- ttyinfo.c_cc[VMIN] = atoi(argv[i+2]) ;
- tcsetattr(0,TCSANOW, &ttyinfo);
- ++argv;
- break;
- case TIME:
- tcgetattr (0, &ttyinfo);
- ttyinfo.c_cc[VTIME] = atoi(argv[i+2]) ;
- tcsetattr(0,TCSANOW, &ttyinfo);
- ++argv;
- break;
- default:
- printf("[ERROR] '%s' is not a valid command.\n", p_command);
- usage();
- }//switch
- ++argv;
- }
- tcgetattr (0, &ttyinfo);
- tcsetattr(0,TCSANOW, &ttyinfo);
- showbaud(cfgetospeed( &ttyinfo));
- show_some_flags( &ttyinfo );
- printf("time = %d \n",ttyinfo.c_cc[VTIME]);
- printf("min = %d \n",ttyinfo.c_cc[VMIN]);
- return(0);
- }//end main();
- void showbaud(int thespeed){
- printf("buad rate = ");
- switch (thespeed ){
- case B300: printf("300\n"); break;
- case B600: printf("600\n"); break;
- case B1200: printf("1200\n"); break;
- case B1800: printf("1800\n"); break;
- case B2400: printf("2400\n"); break;
- case B4800: printf("4800\n"); break;
- case B9600: printf("9600\n"); break;
- default: printf("WARPSPEED\n");break;
- }//switch
- }
- struct charinfo { int fl_value; char *fl_name; char *c; int v; };
- struct flaginfo { int fl_value; char *fl_name; };
- struct flaginfo control_flags[] = {
- { CREAD , "cread "},
- { CRTSCTS , "crtscts"},
- {0 , NULL }};
- struct flaginfo input_flags[] = {
- { IGNBRK , "ignbrk "},
- { ICRNL , "icrnl "},
- { 0 , NULL }};
- struct flaginfo local_flags[] = {
- {ISIG , "isig "},
- {ICANON , "icanon "},
- {ECHO , "echo "},
- {ECHOE , "echoe "},
- {0 , NULL } };
- struct flaginfo output_flags[] = {
- //{OLCUC , "olcuc"},
- { ONLCR , "onlcr "},
- {0 , NULL }};
- struct charinfo special_chars[] ={
- { VERASE , "erase ", "?", 127 },
- { VKILL , "kill ", "U", 85 },
- { VINTR , "intr ", "C", 67 },
- { VSTART , "start ", "Q", 81 },
- { VSTOP , "stop ", "S", 83 },
- {0 , NULL }};
- void show_some_flags( struct termios *ttyinfo )
- {
- show_flagset( ttyinfo->c_cflag, control_flags );
- show_flagset( ttyinfo->c_iflag, input_flags );
- show_flagset( ttyinfo->c_lflag, local_flags );
- show_flagset( ttyinfo->c_oflag, output_flags );
- showchar( ttyinfo->c_cc, special_chars );
- }
- void show_flagset(int thevalue, struct flaginfo thebitnames[]){
- int i;
- for (i=0; thebitnames[i].fl_value; i++ ){
- printf("%s is ", thebitnames[i].fl_name);
- if (thevalue & thebitnames[i].fl_value)
- printf(" ON\n");
- else
- printf(" OFF\n");
- }
- }
- void showchar(int thevalue, struct charinfo thebitnames[]){
- struct termios ttyinfo;
- tcgetattr (0, &ttyinfo);
- tcsetattr(0,TCSANOW, &ttyinfo);
- int i;
- for (i=0; thebitnames[i].fl_value; i++ ){
- tcgetattr (0, &ttyinfo);
- tcsetattr(0,TCSANOW, &ttyinfo);
- int b;
- char* a = thebitnames[i].fl_name;
- b = ttyinfo.c_cc[thebitnames[i].fl_value];
- // printf("%d " ,b);
- if(b < 65 )
- printf("%s is %c \n ", a ,b+'A'-1);
- if(b <127 && b > 65 )
- printf("%s is %c \n", a , b);
- if (b > 127)
- printf("%s is %c \n ", a,b-'A');
- if (b == 127)
- printf("%s is ? \n ", a);
- }
- }
- void usage(void){
- printf("Usage:");
- printf("Eg: -echo <- turns off echo, echo <- turns it back on \n");
- printf("To set ERASE and KILL enter -> erase _ <-and some char \n");
- exit (8);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement