Advertisement
Guest User

Untitled

a guest
May 20th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.00 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6.  
  7. /*
  8. * File: main.c
  9. * Author: kellyst.onge
  10. *
  11. * Created on May 16, 2019, 2:45 PM
  12. */
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <termios.h>
  17. #include <string.h>
  18.  
  19. #define ECH 6385181892
  20. #define ECHOFF 210644657169
  21. #define ICRNLL 210715757085
  22. #define ICRNLLOFF 6951278441226
  23. #define ONLCRON 210723261027
  24. #define ONLCROFF 6951285945168
  25. #define ECHOEON 210711002537
  26. #define ECHOEOFF 6951273686678
  27. #define OLCUCON 210723179931
  28. #define OLCUCOFF 6951285864072
  29. #define CRTSCTSON 229462300234315
  30. #define CRTSCTSOFF 7569935063263864
  31. #define ICANONON 6953619373085
  32. #define ICANOFF 229392187949738
  33. #define ERASECHAR 210711534101
  34. #define KTN 6385404177
  35. #define oops(s,x) { perror(s); exit(x);}
  36.  
  37. int process_command(char *argv[]);
  38. void showbaud(int thespeed);
  39. void show_some_flags( struct termios *ttyp);
  40. void show_flagset();
  41. void showchar();
  42. void usage(void);
  43.  
  44.  
  45. const unsigned long hash(const char *str) {
  46. unsigned long hash = 5381;
  47. int c;
  48.  
  49. while ((c = *str++))
  50. hash = ((hash << 5) + hash) + c;
  51. return hash;
  52. }
  53.  
  54.  
  55. int main(int argc, char *argv[]) {
  56.  
  57.  
  58. struct termios ttyinfo;
  59. int i =1;
  60.  
  61. while (argv[i] != NULL){
  62.  
  63. char *p_command = argv[i];
  64.  
  65. struct termios ttyinfo;
  66.  
  67. if (tcgetattr (0, &ttyinfo) == -1){
  68.  
  69. perror("Cannot get perams about stdin");
  70. exit(1);
  71.  
  72. }
  73.  
  74. // printf("%lu\n",hash(p_command));
  75.  
  76. // printf("switch\n");
  77.  
  78.  
  79. switch(hash(p_command)){
  80.  
  81. struct termios ttyinfo;
  82.  
  83. case ECH:
  84. tcgetattr (0, &ttyinfo);
  85. ttyinfo.c_lflag |= ECHO ;
  86. tcsetattr(0,TCSANOW, &ttyinfo);
  87.  
  88. break;
  89.  
  90. case ECHOFF:
  91. tcgetattr (0, &ttyinfo);
  92. ttyinfo.c_lflag &= ~ECHO ;
  93. tcsetattr(0,TCSANOW, &ttyinfo);
  94.  
  95. break;
  96.  
  97. case ECHOEON:
  98. tcgetattr (0, &ttyinfo);
  99. ttyinfo.c_lflag |= ECHOE ;
  100. tcsetattr(0,TCSANOW, &ttyinfo);
  101.  
  102. break;
  103.  
  104. case ECHOEOFF:
  105. tcgetattr (0, &ttyinfo);
  106. ttyinfo.c_lflag &= ~ECHOE ;
  107. tcsetattr(0,TCSANOW, &ttyinfo);
  108.  
  109. break;
  110.  
  111. case ICRNLL:
  112. tcgetattr (0, &ttyinfo);
  113. ttyinfo.c_iflag |= ICRNL ;
  114. tcsetattr(0,TCSANOW, &ttyinfo);
  115.  
  116. break;
  117.  
  118. case ICRNLLOFF:
  119. tcgetattr (0, &ttyinfo);
  120. ttyinfo.c_iflag &= ~ICRNL ;
  121. tcsetattr(0,TCSANOW, &ttyinfo);
  122.  
  123. break;
  124.  
  125. case ONLCRON:
  126. tcgetattr (0, &ttyinfo);
  127. ttyinfo.c_oflag |= ONLCR ;
  128. tcsetattr(0,TCSANOW, &ttyinfo);
  129.  
  130. break;
  131.  
  132. case ONLCROFF:
  133. tcgetattr (0, &ttyinfo);
  134. ttyinfo.c_oflag &= ~ONLCR ;
  135. tcsetattr(0,TCSANOW, &ttyinfo);
  136.  
  137. break;
  138.  
  139. case CRTSCTSON:
  140. tcgetattr (0, &ttyinfo);
  141. ttyinfo.c_lflag |= CRTSCTS ;
  142. tcsetattr(0,TCSANOW, &ttyinfo);
  143.  
  144. break;
  145.  
  146. case CRTSCTSOFF:
  147. tcgetattr (0, &ttyinfo);
  148. ttyinfo.c_lflag &= ~CRTSCTS ;
  149. tcsetattr(0,TCSANOW, &ttyinfo);
  150.  
  151. break;
  152.  
  153. case OLCUCON:
  154. tcgetattr (0, &ttyinfo);
  155. // ttyinfo.c_oflag |= OLCUC ;
  156. tcsetattr(0,TCSANOW, &ttyinfo);
  157.  
  158. break;
  159.  
  160. case OLCUCOFF:
  161. tcgetattr (0, &ttyinfo);
  162. // ttyinfo.c_oflag &= ~OLCUC ;
  163. tcsetattr(0,TCSANOW, &ttyinfo);
  164.  
  165. break;
  166.  
  167. case ICANONON:
  168. tcgetattr (0, &ttyinfo);
  169. ttyinfo.c_lflag |= ICANON ;
  170. tcsetattr(0,TCSANOW, &ttyinfo);
  171.  
  172. break;
  173.  
  174. case ICANOFF:
  175. tcgetattr (0, &ttyinfo);
  176. ttyinfo.c_lflag &= ~ICANON ;
  177. tcsetattr(0,TCSANOW, &ttyinfo);
  178.  
  179. break;
  180.  
  181. case ERASECHAR:
  182. tcgetattr (0, &ttyinfo);
  183. char* str = argv[i+1];
  184. char d = str[0];
  185. ttyinfo.c_cc[VERASE] = d ;
  186. tcsetattr(0,TCSANOW, &ttyinfo);
  187.  
  188. ++argv;
  189. break;
  190.  
  191. case KTN:
  192. tcgetattr (0, &ttyinfo);
  193. char* strk = argv[i+1];
  194. char k = strk[0];
  195. ttyinfo.c_cc[VKILL] = k ;
  196. tcsetattr(0,TCSANOW, &ttyinfo);
  197.  
  198. ++argv;
  199. break;
  200.  
  201. default:
  202.  
  203. printf("[ERROR] '%s' is not a valid command.\n", p_command);
  204. usage();
  205.  
  206. }//switch
  207. ++argv;
  208.  
  209.  
  210. }
  211.  
  212. tcgetattr (0, &ttyinfo);
  213. tcsetattr(0,TCSANOW, &ttyinfo);
  214. showbaud(cfgetospeed( &ttyinfo));
  215. show_some_flags( &ttyinfo );
  216.  
  217.  
  218.  
  219. printf(" erase = %c\n", ttyinfo.c_cc[VERASE]-1+'A');
  220. printf(" kill = %c\n", ttyinfo.c_cc[VKILL]-1+'A');
  221. printf(" interrupt = %c\n", ttyinfo.c_cc[VINTR]-1+'A');
  222. printf(" start = %c\n", ttyinfo.c_cc[VSTART]-1+'A');
  223. printf(" stop = %c\n", ttyinfo.c_cc[VSTOP]-1+'A');
  224. printf(" werase = %c\n", ttyinfo.c_cc[VWERASE]-1+'A');
  225. printf(" time = %d\n", ttyinfo.c_cc[VTIME]);
  226. printf(" min = %d\n", ttyinfo.c_cc[VMIN]);
  227.  
  228.  
  229. return(0);
  230. }//end main();
  231.  
  232.  
  233. void showbaud(int thespeed){
  234.  
  235. printf("buad rate = ");
  236.  
  237. switch (thespeed ){
  238.  
  239. case B300: printf("300\n"); break;
  240. case B600: printf("600\n"); break;
  241. case B1200: printf("1200\n"); break;
  242. case B1800: printf("1800\n"); break;
  243. case B2400: printf("2400\n"); break;
  244. case B4800: printf("4800\n"); break;
  245. case B9600: printf("9600\n"); break;
  246. default: printf("WARPSPEED\n");break;
  247.  
  248. }//switch
  249.  
  250. }
  251.  
  252.  
  253.  
  254. struct flaginfo { int fl_value; char *fl_name; };
  255. struct flaginfo control_flags[] = {
  256.  
  257. { CREAD , "cread "},
  258. { CRTSCTS , "crtscts"},
  259.  
  260. {0 , NULL }};
  261. struct flaginfo input_flags[] = {
  262. { IGNBRK , "ignbrk "},
  263. { ICRNL , "icrnl "},
  264.  
  265.  
  266. { 0 , NULL }};
  267. struct flaginfo local_flags[] = {
  268. {ISIG , "isig "},
  269. {ICANON , "icanon "},
  270. {ECHO , "echo "},
  271. {ECHOE , "echoe "},
  272. {0 , NULL } };
  273. struct flaginfo output_flags[] = {
  274. //{OLCUC , "olcuc"},
  275. { ONLCR , "onlcr "},
  276.  
  277. {0 , NULL }};
  278.  
  279.  
  280.  
  281. void show_some_flags( struct termios *ttyp )
  282. {
  283. show_flagset( ttyp->c_cflag, control_flags );
  284. show_flagset( ttyp->c_iflag, input_flags );
  285. show_flagset( ttyp->c_lflag, local_flags );
  286. show_flagset( ttyp->c_oflag, output_flags );
  287. // showchar( ttyp->c_cc, special_chars );
  288. }
  289.  
  290. void show_flagset(int thevalue, struct flaginfo thebitnames[]){
  291.  
  292. int i;
  293.  
  294. for (i=0; thebitnames[i].fl_value; i++ ){
  295.  
  296. printf("%s is ", thebitnames[i].fl_name);
  297. if (thevalue & thebitnames[i].fl_value)
  298. printf(" ON\n");
  299. else
  300. printf(" OFF\n");
  301.  
  302. }
  303.  
  304.  
  305. }
  306.  
  307. void showchar(int thevalue, struct flaginfo thebitnames[]){
  308.  
  309.  
  310. int i;
  311.  
  312. for (i=0; thebitnames[i].fl_value; i++ ){
  313.  
  314. printf("%s is ", thebitnames[i].fl_name);
  315. if (thevalue & thebitnames[i].fl_value)
  316. printf("%s", thebitnames[i].fl_name);
  317. else
  318. printf(" OFF\n");
  319.  
  320. }
  321.  
  322.  
  323.  
  324. }
  325.  
  326.  
  327. void usage(void){
  328.  
  329.  
  330. printf("Usage:");
  331. printf("Eg: -echo <- turns off echo, echo <- turns it back on \n");
  332. printf("To set ERASE and KILL enter -> erase _ <-and some char \n");
  333.  
  334. exit (8);
  335. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement