Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.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 //kill
  35. #define INTRC 6385338018
  36. #define STARTT 210728208851
  37. #define STOPP 6385703755
  38. #define MIN 193499273
  39. #define TIME 6385727636
  40. #define oops(s,x) { perror(s); exit(x);}
  41.  
  42. int process_command(char *argv[]);
  43. void showbaud(int thespeed);
  44. void show_some_flags( struct termios *ttyp);
  45. void show_flagset();
  46. void showchar( );
  47. char getchars(char d);
  48. void usage(void);
  49.  
  50.  
  51.  
  52. const unsigned long hash(const char *str) {
  53. unsigned long hash = 5381;
  54. int c;
  55.  
  56. while ((c = *str++))
  57. hash = ((hash << 5) + hash) + c;
  58. return hash;
  59. }
  60.  
  61.  
  62. int main(int argc, char *argv[]) {
  63.  
  64.  
  65. struct termios ttyinfo;
  66. int i =1;
  67.  
  68. while (argv[i] != NULL){
  69.  
  70. char *p_command = argv[i];
  71.  
  72. struct termios ttyinfo;
  73.  
  74. if (tcgetattr (0, &ttyinfo) == -1){
  75.  
  76. perror("Cannot get perams about stdin");
  77. exit(1);
  78.  
  79. }
  80.  
  81.  
  82. // printf("switch\n");
  83.  
  84.  
  85. switch(hash(p_command)){
  86.  
  87. struct termios ttyinfo;
  88.  
  89. case ECH:
  90. tcgetattr (0, &ttyinfo);
  91. ttyinfo.c_lflag |= ECHO ;
  92. tcsetattr(0,TCSANOW, &ttyinfo);
  93.  
  94. break;
  95.  
  96. case ECHOFF:
  97. tcgetattr (0, &ttyinfo);
  98. ttyinfo.c_lflag &= ~ECHO ;
  99. tcsetattr(0,TCSANOW, &ttyinfo);
  100.  
  101. break;
  102.  
  103. case ECHOEON:
  104. tcgetattr (0, &ttyinfo);
  105. ttyinfo.c_lflag |= ECHOE ;
  106. tcsetattr(0,TCSANOW, &ttyinfo);
  107.  
  108. break;
  109.  
  110. case ECHOEOFF:
  111. tcgetattr (0, &ttyinfo);
  112. ttyinfo.c_lflag &= ~ECHOE ;
  113. tcsetattr(0,TCSANOW, &ttyinfo);
  114.  
  115. break;
  116.  
  117. case ICRNLL:
  118. tcgetattr (0, &ttyinfo);
  119. ttyinfo.c_iflag |= ICRNL ;
  120. tcsetattr(0,TCSANOW, &ttyinfo);
  121.  
  122. break;
  123.  
  124. case ICRNLLOFF:
  125. tcgetattr (0, &ttyinfo);
  126. ttyinfo.c_iflag &= ~ICRNL ;
  127. tcsetattr(0,TCSANOW, &ttyinfo);
  128.  
  129. break;
  130.  
  131. case ONLCRON:
  132. tcgetattr (0, &ttyinfo);
  133. ttyinfo.c_oflag |= ONLCR ;
  134. tcsetattr(0,TCSANOW, &ttyinfo);
  135.  
  136. break;
  137.  
  138. case ONLCROFF:
  139. tcgetattr (0, &ttyinfo);
  140. ttyinfo.c_oflag &= ~ONLCR ;
  141. tcsetattr(0,TCSANOW, &ttyinfo);
  142.  
  143. break;
  144.  
  145. case CRTSCTSON:
  146. tcgetattr (0, &ttyinfo);
  147. ttyinfo.c_lflag |= CRTSCTS ;
  148. tcsetattr(0,TCSANOW, &ttyinfo);
  149.  
  150. break;
  151.  
  152. case CRTSCTSOFF:
  153. tcgetattr (0, &ttyinfo);
  154. ttyinfo.c_lflag &= ~CRTSCTS ;
  155. tcsetattr(0,TCSANOW, &ttyinfo);
  156.  
  157. break;
  158.  
  159. case OLCUCON:
  160. tcgetattr (0, &ttyinfo);
  161. // ttyinfo.c_oflag |= OLCUC ;
  162. tcsetattr(0,TCSANOW, &ttyinfo);
  163.  
  164. break;
  165.  
  166. case OLCUCOFF:
  167. tcgetattr (0, &ttyinfo);
  168. // ttyinfo.c_oflag &= ~OLCUC ;
  169. tcsetattr(0,TCSANOW, &ttyinfo);
  170.  
  171. break;
  172.  
  173. case ICANONON:
  174. tcgetattr (0, &ttyinfo);
  175. ttyinfo.c_lflag |= ICANON ;
  176. tcsetattr(0,TCSANOW, &ttyinfo);
  177.  
  178. break;
  179.  
  180. case ICANOFF:
  181. tcgetattr (0, &ttyinfo);
  182. ttyinfo.c_lflag &= ~ICANON ;
  183. tcsetattr(0,TCSANOW, &ttyinfo);
  184.  
  185. break;
  186.  
  187. case ERASECHAR:
  188. tcgetattr (0, &ttyinfo);
  189. char* str = argv[i+1];
  190. char d = str[0];
  191. ttyinfo.c_cc[VERASE] = d ;
  192. tcsetattr(0,TCSANOW, &ttyinfo);
  193.  
  194.  
  195. ++argv;
  196. break;
  197.  
  198. case KTN:
  199. tcgetattr (0, &ttyinfo);
  200. char* strk = argv[i+1];
  201. char k = strk[0];
  202. ttyinfo.c_cc[VKILL] = k ;
  203. tcsetattr(0,TCSANOW, &ttyinfo);
  204.  
  205. ++argv;
  206. break;
  207.  
  208. case INTRC:
  209. tcgetattr (0, &ttyinfo);
  210. char* strc = argv[i+1];
  211. char i = strc[0];
  212. ttyinfo.c_cc[VINTR] = i ;
  213. tcsetattr(0,TCSANOW, &ttyinfo);
  214.  
  215. ++argv;
  216. break;
  217.  
  218. case STARTT:
  219. tcgetattr (0, &ttyinfo);
  220. char* strs = argv[i+2];
  221. char x = strs[0];
  222. ttyinfo.c_cc[VSTART] = x ;
  223. tcsetattr(0,TCSANOW, &ttyinfo);
  224.  
  225. ++argv;
  226. break;
  227.  
  228. case STOPP:
  229. tcgetattr (0, &ttyinfo);
  230. char* sto = argv[i+2];
  231. char y = sto[0];
  232. ttyinfo.c_cc[VSTOP] = y ;
  233. tcsetattr(0,TCSANOW, &ttyinfo);
  234.  
  235. ++argv;
  236. break;
  237.  
  238. case MIN:
  239. tcgetattr (0, &ttyinfo);
  240.  
  241.  
  242. ttyinfo.c_cc[VMIN] = atoi(argv[i+2]) ;
  243. tcsetattr(0,TCSANOW, &ttyinfo);
  244.  
  245. ++argv;
  246. break;
  247.  
  248. case TIME:
  249. tcgetattr (0, &ttyinfo);
  250.  
  251.  
  252. ttyinfo.c_cc[VTIME] = atoi(argv[i+2]) ;
  253. tcsetattr(0,TCSANOW, &ttyinfo);
  254.  
  255. ++argv;
  256. break;
  257. default:
  258.  
  259. printf("[ERROR] '%s' is not a valid command.\n", p_command);
  260. usage();
  261.  
  262. }//switch
  263.  
  264. ++argv;
  265.  
  266.  
  267. }
  268. tcgetattr (0, &ttyinfo);
  269. tcsetattr(0,TCSANOW, &ttyinfo);
  270.  
  271. showbaud(cfgetospeed( &ttyinfo));
  272. show_some_flags( &ttyinfo );
  273.  
  274. printf("time = %d \n",ttyinfo.c_cc[VTIME]);
  275. printf("min = %d \n",ttyinfo.c_cc[VMIN]);
  276.  
  277.  
  278. return(0);
  279. }//end main();
  280.  
  281.  
  282. void showbaud(int thespeed){
  283.  
  284. printf("buad rate = ");
  285.  
  286. switch (thespeed ){
  287.  
  288. case B300: printf("300\n"); break;
  289. case B600: printf("600\n"); break;
  290. case B1200: printf("1200\n"); break;
  291. case B1800: printf("1800\n"); break;
  292. case B2400: printf("2400\n"); break;
  293. case B4800: printf("4800\n"); break;
  294. case B9600: printf("9600\n"); break;
  295. default: printf("WARPSPEED\n");break;
  296.  
  297. }//switch
  298.  
  299. }
  300.  
  301.  
  302. struct charinfo { int fl_value; char *fl_name; char *c; int v; };
  303. struct flaginfo { int fl_value; char *fl_name; };
  304. struct flaginfo control_flags[] = {
  305.  
  306. { CREAD , "cread "},
  307. { CRTSCTS , "crtscts"},
  308.  
  309. {0 , NULL }};
  310. struct flaginfo input_flags[] = {
  311. { IGNBRK , "ignbrk "},
  312. { ICRNL , "icrnl "},
  313.  
  314.  
  315. { 0 , NULL }};
  316. struct flaginfo local_flags[] = {
  317. {ISIG , "isig "},
  318. {ICANON , "icanon "},
  319. {ECHO , "echo "},
  320. {ECHOE , "echoe "},
  321. {0 , NULL } };
  322. struct flaginfo output_flags[] = {
  323. //{OLCUC , "olcuc"},
  324. { ONLCR , "onlcr "},
  325.  
  326. {0 , NULL }};
  327. struct charinfo special_chars[] ={
  328.  
  329.  
  330. { VERASE , "erase ", "?", 127 },
  331. { VKILL , "kill ", "U", 85 },
  332. { VINTR , "intr ", "C", 67 },
  333. { VSTART , "start ", "Q", 81 },
  334. { VSTOP , "stop ", "S", 83 },
  335.  
  336.  
  337. {0 , NULL }};
  338.  
  339.  
  340.  
  341.  
  342.  
  343. void show_some_flags( struct termios *ttyinfo )
  344. {
  345.  
  346. show_flagset( ttyinfo->c_cflag, control_flags );
  347. show_flagset( ttyinfo->c_iflag, input_flags );
  348. show_flagset( ttyinfo->c_lflag, local_flags );
  349. show_flagset( ttyinfo->c_oflag, output_flags );
  350. showchar( ttyinfo->c_cc, special_chars );
  351. }
  352.  
  353. void show_flagset(int thevalue, struct flaginfo thebitnames[]){
  354.  
  355. int i;
  356.  
  357. for (i=0; thebitnames[i].fl_value; i++ ){
  358.  
  359. printf("%s is ", thebitnames[i].fl_name);
  360. if (thevalue & thebitnames[i].fl_value)
  361. printf(" ON\n");
  362. else
  363. printf(" OFF\n");
  364.  
  365. }
  366.  
  367.  
  368. }
  369.  
  370. void showchar(int thevalue, struct charinfo thebitnames[]){
  371.  
  372. struct termios ttyinfo;
  373. tcgetattr (0, &ttyinfo);
  374. tcsetattr(0,TCSANOW, &ttyinfo);
  375.  
  376.  
  377.  
  378. int i;
  379.  
  380. for (i=0; thebitnames[i].fl_value; i++ ){
  381.  
  382. tcgetattr (0, &ttyinfo);
  383. tcsetattr(0,TCSANOW, &ttyinfo);
  384. int b;
  385.  
  386. char* a = thebitnames[i].fl_name;
  387. b = ttyinfo.c_cc[thebitnames[i].fl_value];
  388.  
  389. // printf("%d " ,b);
  390.  
  391. if(b < 65 )
  392.  
  393. printf("%s is %c \n ", a ,b+'A'-1);
  394.  
  395. if(b <127 && b > 65 )
  396.  
  397. printf("%s is %c \n", a , b);
  398.  
  399. if (b > 127)
  400.  
  401. printf("%s is %c \n ", a,b-'A');
  402.  
  403. if (b == 127)
  404.  
  405. printf("%s is ? \n ", a);
  406.  
  407. }
  408.  
  409.  
  410.  
  411. }
  412.  
  413.  
  414.  
  415.  
  416. void usage(void){
  417.  
  418.  
  419. printf("Usage:");
  420. printf("Eg: -echo <- turns off echo, echo <- turns it back on \n");
  421. printf("To set ERASE and KILL enter -> erase _ <-and some char \n");
  422.  
  423. exit (8);
  424. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement