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. int join_session(int sid);
  52. struct cmdtable {
  53. char *cmd;
  54. int (* do_cmd) (char *);
  55. char *help;
  56. } cmdtab[] = {
  57. { "-help", do_cmd_help, "Show information about every message. " },
  58. { "-quit", do_cmd_quit, "Disconnect to server and exit vcc. " },
  59. { "-ls", do_cmd_ls, "List all users online now. " },
  60. { "-rate", do_cmd_rate, "Change message refresh rate. " },
  61. { "-newse", do_cmd_newse, "Create a new session. " },
  62. { "-currs", do_cmd_currs, "Get current session id. " },
  63. { "-swtch", do_cmd_swtch, "Switch to another session. " },
  64. { "-lsse", do_cmd_lsse, "List all sessions created. " },
  65. { NULL, NULL, NULL }
  66. };
  67. #define clear_line() fprintf(stderr, "\033[2K\r")
  68. #define __unused __attribute__((unused))
  69. int do_cmd(char *s) {
  70. struct cmdtable *t;
  71. char *p;
  72. int len;
  73. p = s;
  74. for (len = 0; !isspace(*p); len++)
  75. p++;
  76. for (t = cmdtab; t->cmd; t++)
  77. if (!strncmp(t->cmd, s, len))
  78. return t->do_cmd(s);
  79. pretty_unknow_cmd(s);
  80. return 0;
  81. }
  82. int do_cmd_help(char *s) {
  83. struct cmdtable *t;
  84. (void) s;
  85. for (t = cmdtab; t->cmd; t++)
  86. pretty_cmd_help(t->cmd, t->help);
  87. return 0;
  88. }
  89. int do_cmd_quit(char *s) {
  90. (void) s;
  91. printf("bye. \n");
  92. exit(0);
  93. return 0;
  94. }
  95. int do_cmd_ls(char *s) {
  96. (void) s;
  97. get_users();
  98. return 0;
  99. }
  100. int do_cmd_rate(char *s) {
  101. int n;
  102. (void) s;
  103. printf("Current rate: %d Hz\n", rate / 1000);
  104. fprintf(stderr, "New rate (0 < rate <= 100): ");
  105. scanf("%d", &n);
  106. if (n > 100) {
  107. fprintf(stderr, "*** too fast. \n"
  108. "0 < rate <= 100\n");
  109. return 1;
  110. }
  111. if (n <= 0) {
  112. fprintf(stderr, "*** too slow. \n"
  113. "0 < rate <= 100\n");
  114. return 1;
  115. }
  116. rate = 1000 / n;
  117. printf("New rate: %d Hz\n", n);
  118. return 0;
  119. }
  120. int do_cmd_ls_bh(struct vcc_request *req) {
  121. int n, i;
  122. n = ntohl(req->uid);
  123. clear_line();
  124. if (unlikely(ntohl(req->reqtype) != REQ_CTL_USRS)) {
  125. fprintf(stderr, "--- do_cmd_ls_bh: bad package. \n");
  126. return 1;
  127. }
  128. for (i = 0; i < n; i++)
  129. printf("%s\n", req->msg + i * USRNAME_SIZE);
  130. pretty_prompt(usrname);
  131. return 0;
  132. }
  133. int do_cmd_lsse(char *s) {
  134. (void) s;
  135. get_sessions();
  136. return 0;
  137. }
  138. int do_cmd_lsse_bh(struct vcc_request *req) {
  139. int n, i;
  140. n = ntohl(req->uid);
  141. clear_line();
  142. if (unlikely(ntohl(req->reqtype) != REQ_CTL_SESS)) {
  143. fprintf(stderr, "--- do_cmd_ls_bh: bad package. \n");
  144. return 1;
  145. }
  146. for (i = 0; i < n; i++)
  147. printf("%s\n", req->msg + i * USRNAME_SIZE);
  148. pretty_prompt(usrname);
  149. return 0;
  150. }
  151. int do_cmd_newse(char *s) {
  152. char sname[USRNAME_SIZE];
  153. (void) s;
  154. fprintf(stderr, "Name of new session: ");
  155. scanf("%s", sname);
  156. vcc_create_session(sname);
  157. printf("Session create request sent. \n");
  158. return 0;
  159. }
  160. int do_cmd_currs(char *s) {
  161. (void) s;
  162. printf("Current session id: %d\n", current_sid);
  163. return 0;
  164. }
  165. int do_cmd_swtch(char *s) {
  166. int sid;
  167. (void) s;
  168. printf("Old session id: %d\n", current_sid);
  169. fprintf(stderr, "New session id: ");
  170. scanf("%d", &sid);
  171. current_sid = sid;
  172. join_session(current_sid);
  173. return 0;
  174. }