cmd.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 read_msg(struct vcc_request *);
  39. extern char usrname[];
  40. int get_users(void);
  41. struct cmdtable {
  42. char *cmd;
  43. int (* do_cmd) (char *);
  44. char *help;
  45. } cmdtab[] = {
  46. { "-help", do_cmd_help, "Show information about every message. " },
  47. { "-quit", do_cmd_quit, "Disconnect to server and exit vcc. " },
  48. { "-ls", do_cmd_ls, "List all users online now. " },
  49. { NULL, NULL, NULL }
  50. };
  51. #define clear_line() fprintf(stderr, "\033[2K\r")
  52. int do_cmd(char *s) {
  53. struct cmdtable *t;
  54. for (t = cmdtab; t->cmd; t++)
  55. if (!strncmp(t->cmd, s, strlen(t->cmd)))
  56. return t->do_cmd(s);
  57. pretty_unknow_cmd(s);
  58. return 0;
  59. }
  60. int do_cmd_help(char *s) {
  61. struct cmdtable *t;
  62. for (t = cmdtab; t->cmd; t++)
  63. pretty_cmd_help(t->cmd, t->help);
  64. return 0;
  65. }
  66. int do_cmd_quit(char *s) {
  67. printf("bye. \n");
  68. exit(0);
  69. return 0;
  70. }
  71. int do_cmd_ls(char *s) {
  72. get_users();
  73. return 0;
  74. }
  75. int do_cmd_ls_bh(struct vcc_request *req) {
  76. int n, i;
  77. n = ntohl(req->uid);
  78. clear_line();
  79. if (unlikely(ntohl(req->reqtype) != REQ_CTL_USRS)) {
  80. fprintf(stderr, "--- do_cmd_ls_bh: bad package. \n");
  81. return 1;
  82. }
  83. for (i = 0; i < n; i++)
  84. printf("%s\n", req->msg + i * USRNAME_SIZE);
  85. pretty_prompt(usrname);
  86. return 0;
  87. }