cmd.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. #define __unused __attribute__((unused))
  56. int do_cmd(char *s) {
  57. struct cmdtable *t;
  58. (void) s;
  59. for (t = cmdtab; t->cmd; t++)
  60. if (!strncmp(t->cmd, s, strlen(t->cmd)))
  61. return t->do_cmd(s);
  62. pretty_unknow_cmd(s);
  63. return 0;
  64. }
  65. int do_cmd_help(char *s) {
  66. struct cmdtable *t;
  67. (void) s;
  68. for (t = cmdtab; t->cmd; t++)
  69. pretty_cmd_help(t->cmd, t->help);
  70. return 0;
  71. }
  72. int do_cmd_quit(char *s) {
  73. (void) s;
  74. printf("bye. \n");
  75. exit(0);
  76. return 0;
  77. }
  78. int do_cmd_ls(char *s) {
  79. (void) s;
  80. get_users();
  81. return 0;
  82. }
  83. int do_cmd_rate(char *s) {
  84. int n;
  85. (void) s;
  86. printf("Current rate: %d Hz\n", rate / 1000);
  87. fprintf(stderr, "New rate (0 < rate <= 100): ");
  88. scanf("%d", &n);
  89. if (n > 100) {
  90. fprintf(stderr, "*** too fast. \n"
  91. "0 < rate <= 100\n");
  92. return 1;
  93. }
  94. if (n <= 0) {
  95. fprintf(stderr, "*** too slow. \n"
  96. "0 < rate <= 100\n");
  97. return 1;
  98. }
  99. rate = 1000 / n;
  100. printf("New rate: %d Hz\n", n);
  101. return 0;
  102. }
  103. int do_cmd_ls_bh(struct vcc_request *req) {
  104. int n, i;
  105. n = ntohl(req->uid);
  106. clear_line();
  107. if (unlikely(ntohl(req->reqtype) != REQ_CTL_USRS)) {
  108. fprintf(stderr, "--- do_cmd_ls_bh: bad package. \n");
  109. return 1;
  110. }
  111. for (i = 0; i < n; i++)
  112. printf("%s\n", req->msg + i * USRNAME_SIZE);
  113. pretty_prompt(usrname);
  114. return 0;
  115. }