cmd.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 <sys/stat.h>
  25. #include <sys/types.h>
  26. #include <sys/socket.h>
  27. #include <netinet/in.h>
  28. #include <arpa/inet.h>
  29. #include <poll.h>
  30. #include <signal.h>
  31. #include <vcc/vcc.h>
  32. #include <vcc/version.h>
  33. #include <vcc/pretty.h>
  34. int do_cmd_help(char *);
  35. int do_cmd_quit(char *);
  36. int do_cmd_ls(char *);
  37. int do_cmd_ls_bh(struct vcc_request *);
  38. int do_cmd_rate(char *);
  39. int read_msg(struct vcc_request *);
  40. extern char usrname[];
  41. extern int rate;
  42. int get_users(void);
  43. struct cmdtable {
  44. char *cmd;
  45. int (* do_cmd) (char *);
  46. char *help;
  47. } cmdtab[] = {
  48. { "-help", do_cmd_help, "Show information about every message. " },
  49. { "-quit", do_cmd_quit, "Disconnect to server and exit vcc. " },
  50. { "-ls", do_cmd_ls, "List all users online now. " },
  51. { "-rate", do_cmd_rate, "Change message refresh rate. " },
  52. { NULL, NULL, NULL }
  53. };
  54. #define clear_line() fprintf(stderr, "\033[2K\r")
  55. int do_cmd(char *s) {
  56. struct cmdtable *t;
  57. for (t = cmdtab; t->cmd; t++)
  58. if (!strncmp(t->cmd, s, strlen(t->cmd)))
  59. return t->do_cmd(s);
  60. pretty_unknow_cmd(s);
  61. return 0;
  62. }
  63. int do_cmd_help(char *s) {
  64. struct cmdtable *t;
  65. for (t = cmdtab; t->cmd; t++)
  66. pretty_cmd_help(t->cmd, t->help);
  67. return 0;
  68. }
  69. int do_cmd_quit(char *s) {
  70. printf("bye. \n");
  71. exit(0);
  72. return 0;
  73. }
  74. int do_cmd_ls(char *s) {
  75. get_users();
  76. return 0;
  77. }
  78. int do_cmd_rate(char *s) {
  79. int n;
  80. printf("Current rate: %d Hz\n", rate / 1000);
  81. fprintf(stderr, "New rate (0 < rate <= 100): ");
  82. scanf("%d", &n);
  83. if (n > 100) {
  84. fprintf(stderr, "*** too fast. \n"
  85. "0 < rate <= 100\n");
  86. return 1;
  87. }
  88. if (n <= 0) {
  89. fprintf(stderr, "*** too slow. \n"
  90. "0 < rate <= 100\n");
  91. return 1;
  92. }
  93. rate = 1000 / n;
  94. printf("New rate: %d Hz\n", n);
  95. return 0;
  96. }
  97. int do_cmd_ls_bh(struct vcc_request *req) {
  98. int n, i;
  99. n = ntohl(req->uid);
  100. clear_line();
  101. if (unlikely(ntohl(req->reqtype) != REQ_CTL_USRS)) {
  102. fprintf(stderr, "--- do_cmd_ls_bh: bad package. \n");
  103. return 1;
  104. }
  105. for (i = 0; i < n; i++)
  106. printf("%s\n", req->msg + i * USRNAME_SIZE);
  107. pretty_prompt(usrname);
  108. return 0;
  109. }