| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- /* vcc/cmd.c
- *
- * This file is part of vcc.
- *
- * vcc is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * vcc is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with vcc. If not, see <https://www.gnu.org/licenses/>
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <string.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <poll.h>
- #include <signal.h>
- #include <vcc/vcc.h>
- #include <vcc/version.h>
- #include <vcc/pretty.h>
- int do_cmd_help(char *);
- int do_cmd_quit(char *);
- int do_cmd_ls(char *);
- int do_cmd_ls_bh(struct vcc_request *);
- extern int pid;
- extern char usrname[];
- int get_users(void);
- struct cmdtable {
- char *cmd;
- int (* do_cmd) (char *);
- char *help;
- } cmdtab[] = {
- { "-help", do_cmd_help, "Show information about every message. " },
- { "-quit", do_cmd_quit, "Disconnect to server and exit vcc. " },
- { "-ls", do_cmd_ls, "List all users online now. " },
- { NULL, NULL, NULL }
- };
- void *bh_list[] = {do_cmd_ls_bh};
- int do_cmd(char *s) {
- struct cmdtable *t;
- for (t = cmdtab; t->cmd; t++)
- if (!strncmp(t->cmd, s, strlen(t->cmd)))
- return t->do_cmd(s);
-
- pretty_unknow_cmd(s);
- return 0;
- }
- /* bottom half */
- int do_cmd_bh(struct vcc_request *req) {
- int i;
- int (* fn) (struct vcc_request *);
- for (i = 0; bh_list[i]; i++)
- if (!(fn = bh_list[i])(req)) {
- pretty_prompt(usrname);
- return 0;
- }
- fprintf(stderr, "--- unexpected bh\n");
- return 1;
- }
- int do_cmd_help(char *s) {
- struct cmdtable *t;
- for (t = cmdtab; t->cmd; t++)
- pretty_cmd_help(t->cmd, t->help);
-
- return 0;
- }
- int do_cmd_quit(char *s) {
- printf("bye. \n");
- exit(0);
- return 0;
- }
- int do_cmd_ls_bh(struct vcc_request *req) {
- int i, n = ntohl(req->uid);
- /* clear current line */
- printf("\033[2K\r");
- if (unlikely(ntohl(req->reqtype) != REQ_CTL_USRS)) {
- fprintf(stderr, "--- do_cmd_ls_bh: bad package. \n");
- return 1;
- }
- for (i = 0; i < n; i++)
- printf("%s\n", req->msg + i * USRNAME_SIZE);
- return 0;
- }
- int do_cmd_ls(char *s) {
- get_users();
-
- return 0;
- }
|