cmd.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* vcc/cmd.c
  2. *
  3. * This file is part of vcc.
  4. *
  5. * vcc is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * vcc is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with vcc. If not, see <https://www.gnu.org/licenses/>
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <fcntl.h>
  22. #include <errno.h>
  23. #include <string.h>
  24. #include <ctype.h>
  25. #include <sys/stat.h>
  26. #include <sys/types.h>
  27. #include <sys/socket.h>
  28. #include <netinet/in.h>
  29. #include <arpa/inet.h>
  30. #include <poll.h>
  31. #include <signal.h>
  32. #include <vcc/vcc.h>
  33. #include <vcc/version.h>
  34. #include <vcc/pretty.h>
  35. int do_cmd_help(char *);
  36. int do_cmd_quit(char *);
  37. int do_cmd_ls(char *);
  38. int do_cmd_rate(char *);
  39. int do_cmd_newse(char *);
  40. int do_cmd_currs(char *);
  41. int do_cmd_swtch(char *);
  42. int do_cmd_lsse(char *);
  43. int read_msg(struct vcc_request *);
  44. int vcc_create_session(char *name);
  45. int do_cmd_ls_bh(struct vcc_request *);
  46. extern char usrname[];
  47. extern int rate;
  48. extern int current_sid;
  49. int get_users(void);
  50. int get_sessions(void);
  51. struct cmdtable {
  52. char *cmd;
  53. int (* do_cmd) (char *);
  54. char *help;
  55. } cmdtab[] = {
  56. { "-help", do_cmd_help, "Show information about every message. " },
  57. { "-quit", do_cmd_quit, "Disconnect to server and exit vcc. " },
  58. { "-ls", do_cmd_ls, "List all users online now. " },
  59. { "-rate", do_cmd_rate, "Change message refresh rate. " },
  60. { "-newse", do_cmd_newse, "Create a new session. " },
  61. { "-currs", do_cmd_currs, "Get current session id. " },
  62. { "-swtch", do_cmd_swtch, "Switch to another session. " },
  63. { "-lsse", do_cmd_lsse, "List all sessions created. " },
  64. { NULL, NULL, NULL }
  65. };
  66. #define clear_line() fprintf(stderr, "\033[2K\r")
  67. #define __unused __attribute__((unused))
  68. int do_cmd(char *s) {
  69. struct cmdtable *t;
  70. char *p;
  71. int len;
  72. p = s;
  73. for (len = 0; !isspace(*p); len++)
  74. p++;
  75. for (t = cmdtab; t->cmd; t++)
  76. if (!strncmp(t->cmd, s, len))
  77. return t->do_cmd(s);
  78. pretty_unknow_cmd(s);
  79. return 0;
  80. }
  81. int do_cmd_help(char *s) {
  82. struct cmdtable *t;
  83. (void) s;
  84. for (t = cmdtab; t->cmd; t++)
  85. pretty_cmd_help(t->cmd, t->help);
  86. return 0;
  87. }
  88. int do_cmd_quit(char *s) {
  89. (void) s;
  90. printf("bye. \n");
  91. exit(0);
  92. return 0;
  93. }
  94. int do_cmd_ls(char *s) {
  95. (void) s;
  96. get_users();
  97. return 0;
  98. }
  99. int do_cmd_rate(char *s) {
  100. int n;
  101. (void) s;
  102. printf("Current rate: %d Hz\n", rate / 1000);
  103. fprintf(stderr, "New rate (0 < rate <= 100): ");
  104. scanf("%d", &n);
  105. if (n > 100) {
  106. fprintf(stderr, "*** too fast. \n"
  107. "0 < rate <= 100\n");
  108. return 1;
  109. }
  110. if (n <= 0) {
  111. fprintf(stderr, "*** too slow. \n"
  112. "0 < rate <= 100\n");
  113. return 1;
  114. }
  115. rate = 1000 / n;
  116. printf("New rate: %d Hz\n", n);
  117. return 0;
  118. }
  119. int do_cmd_ls_bh(struct vcc_request *req) {
  120. int n, i;
  121. n = ntohl(req->uid);
  122. clear_line();
  123. if (unlikely(ntohl(req->reqtype) != REQ_CTL_USRS)) {
  124. fprintf(stderr, "--- do_cmd_ls_bh: bad package. \n");
  125. return 1;
  126. }
  127. for (i = 0; i < n; i++)
  128. printf("%s\n", req->msg + i * USRNAME_SIZE);
  129. pretty_prompt(usrname);
  130. return 0;
  131. }
  132. int do_cmd_lsse(char *s) {
  133. (void) s;
  134. get_sessions();
  135. return 0;
  136. }
  137. int do_cmd_lsse_bh(struct vcc_request *req) {
  138. int n, i;
  139. n = ntohl(req->uid);
  140. clear_line();
  141. if (unlikely(ntohl(req->reqtype) != REQ_CTL_SESS)) {
  142. fprintf(stderr, "--- do_cmd_ls_bh: bad package. \n");
  143. return 1;
  144. }
  145. for (i = 0; i < n; i++)
  146. printf("%s\n", req->msg + i * USRNAME_SIZE);
  147. pretty_prompt(usrname);
  148. return 0;
  149. }
  150. int do_cmd_newse(char *s) {
  151. char sname[USRNAME_SIZE];
  152. (void) s;
  153. fprintf(stderr, "Name of new session: ");
  154. scanf("%s", sname);
  155. vcc_create_session(sname);
  156. printf("Session create request sent. \n");
  157. return 0;
  158. }
  159. int do_cmd_currs(char *s) {
  160. (void) s;
  161. printf("Current session id: %d\n", current_sid);
  162. return 0;
  163. }
  164. int do_cmd_swtch(char *s) {
  165. int sid;
  166. (void) s;
  167. printf("Old session id: %d\n", current_sid);
  168. fprintf(stderr, "New session id: ");
  169. scanf("%d", &sid);
  170. current_sid = sid;
  171. printf("We'll use this session from now on. \n");
  172. return 0;
  173. }