cmd.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. int do_cmd(char *s) {
  52. struct cmdtable *t;
  53. for (t = cmdtab; t->cmd; t++)
  54. if (!strncmp(t->cmd, s, strlen(t->cmd)))
  55. return t->do_cmd(s);
  56. pretty_unknow_cmd(s);
  57. return 0;
  58. }
  59. int do_cmd_help(char *s) {
  60. struct cmdtable *t;
  61. for (t = cmdtab; t->cmd; t++)
  62. pretty_cmd_help(t->cmd, t->help);
  63. return 0;
  64. }
  65. int do_cmd_quit(char *s) {
  66. printf("bye. \n");
  67. exit(0);
  68. return 0;
  69. }
  70. int do_cmd_ls(char *s) {
  71. struct vcc_request req;
  72. int i, n;
  73. get_users();
  74. read_msg(&req);
  75. n = ntohl(req.uid);
  76. /* clear current line */
  77. if (unlikely(ntohl(req.reqtype) != REQ_CTL_USRS)) {
  78. fprintf(stderr, "--- do_cmd_ls_bh: bad package. \n");
  79. return 1;
  80. }
  81. for (i = 0; i < n; i++)
  82. printf("%s\n", req.msg + i * USRNAME_SIZE);
  83. return 0;
  84. }