Quellcode durchsuchen

vcc: support commands but something is wrong about prompt

intirain vor 4 Jahren
Ursprung
Commit
0dc9860114
8 geänderte Dateien mit 253 neuen und 14 gelöschten Zeilen
  1. 3 3
      Makefile
  2. 145 0
      cmd.c
  3. 6 2
      include/vcc/pretty.h
  4. 3 0
      include/vcc/vcc.h
  5. 1 1
      include/vcc/version.h
  6. 35 8
      main.c
  7. 45 0
      pretty.c
  8. 15 0
      vcc.c

+ 3 - 3
Makefile

@@ -1,11 +1,11 @@
 VCCD_MAJOR 		= 0
-VCCD_MINOR 		= 4
+VCCD_MINOR 		= 5
 VCCD_PATCH 		= 0
 VCCD_COMPILE_TIME 	= $(shell date)
 VCCD_COMPILE_BY 	= $(shell whoami)
 
-srcs 			= main.c vcc.c pretty.c 
-objs 			= main.o vcc.o pretty.o 
+srcs 			= main.c vcc.c pretty.c cmd.c 
+objs 			= main.o vcc.o pretty.o cmd.o 
 target 			= vcc 
 CFLAGS 			= -Wall -Werror -Iinclude -g 
 MAKEFLAGS 		= --no-print-directory

+ 145 - 0
cmd.c

@@ -0,0 +1,145 @@
+/* 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");
+
+	kill(pid, SIGUSR1);
+	exit(0);
+
+	return 0;
+}
+
+
+int do_cmd_ls_bh(struct vcc_request *req) {
+	int i, n = ntohl(req->uid);
+
+	if (unlikely(ntohl(req->reqtype) != REQ_CTL_USRS)) {
+		fprintf(stderr, "--- do_cmd_ls_bh: bad package. \n");
+
+		return 1;
+	}
+
+	printf("\n");
+
+	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;
+}
+
+
+
+

+ 6 - 2
include/vcc/pretty.h

@@ -64,13 +64,17 @@ struct theme {
 	struct color 	usrname, 
 			sysusername, 
 			msg, 
-			sysmsg;
+			sysmsg, 
+			errmsg, 
+			helpcmd, 
+			helpmsg;
 };
 
 
 int pretty_new_msg(char *usrname, char *msg);
 int pretty_prompt(char *usrname);
-
+int pretty_unknow_cmd(char *cmd);
+int pretty_cmd_help(char *cmd, char *msg);
 
 
 #endif

+ 3 - 0
include/vcc/vcc.h

@@ -34,6 +34,9 @@
 /* server -> client. sent when received a message */
 #define REQ_MSG_NEW 	2
 
+/* client -> server. get users logged-in */
+#define REQ_CTL_USRS 	3
+
 #define REQ_SIZE 	4096
 #define USRNAME_SIZE 	32
 #define MSG_SIZE 	(REQ_SIZE - 3 * sizeof(int) - USRNAME_SIZE)

+ 1 - 1
include/vcc/version.h

@@ -1 +1 @@
-#define VCCD_VERSION "vccd 0.4.0 Sun Mar  6 13:04:25 CST 2022 root"
+#define VCCD_VERSION "vccd 0.5.0 Sun Mar  6 14:51:00 CST 2022 root"

+ 35 - 8
main.c

@@ -42,9 +42,13 @@
 int send_msg(const char *msg, const char *usrname);
 int read_msg(struct vcc_request *buf);
 
+int do_cmd(char *s);
+int do_cmd_bh(struct vcc_request *);
 
-int fd;
+
+int fd, pid;
 char *empty_package;
+char usrname[USRNAME_SIZE];
 
 static int usage(void) {
 	fprintf(stderr, "Usage: vcc [server]\n");
@@ -104,7 +108,18 @@ void alarm_handler(int nr) {
 	return;
 }
 
-static int setup_alarm(void) {
+
+void usr1_handler(int nr) {
+	printf("bye. \n");
+
+	close(fd);
+	exit(0);
+
+	return;
+}
+
+
+static int setup_signals(void) {
 	struct sigaction act;
 
 	if (unlikely(!(empty_package = malloc(MSG_SIZE)))) {
@@ -120,6 +135,11 @@ static int setup_alarm(void) {
 
 	sigaction(SIGALRM, &act, NULL);
 
+	act.sa_handler = usr1_handler;
+	act.sa_flags = 0;
+
+	sigaction(SIGUSR1, &act, NULL);
+
 	alarm(1);
 
 	return 0;
@@ -129,8 +149,6 @@ static int setup_alarm(void) {
 int main(int argc, const char **argv) {
 	struct vcc_request req;
 	char *buf;
-	char usrname[USRNAME_SIZE];
-	int pid;
 
 	if (unlikely(argc != 1 && argc != 2)) 
 		return usage();
@@ -143,10 +161,10 @@ int main(int argc, const char **argv) {
 	fprintf(stderr, "login as: ");
 	scanf("%s", usrname);
 
-	if ((pid = fork())) {
+	if (!(pid = fork())) {
 		/* receive messages */
 
-		setup_alarm();
+		setup_signals();
 
 		for (;;) {
 			if (read_msg(&req) == -1) {
@@ -155,6 +173,12 @@ int main(int argc, const char **argv) {
 				break;
 			}
 
+			if (ntohl(req.reqtype) != REQ_MSG_NEW) {
+				do_cmd_bh(&req);
+
+				continue;
+			}
+
 			buf = req.msg;
 
 			if (!*buf || *(unsigned char *) buf == 0xffu) 
@@ -186,8 +210,11 @@ int main(int argc, const char **argv) {
 			if (*buf == '\n') 
 				continue;
 
-			if (!strcmp(buf, "quit\n")) 
-				break;
+			if (*buf == '-') {
+				do_cmd(buf);
+
+				continue;
+			}
 
 			send_msg(buf, usrname);
 		}

+ 45 - 0
pretty.c

@@ -50,6 +50,24 @@ struct theme theme_def = {
 		.fg = FG_GREEN, 
 		.bg = BG_BLACK, 
 		.mode = MODE_HIGHLIGHT
+	}, 
+	
+	.errmsg = {
+		.fg = FG_RED, 
+		.bg = BG_BLACK, 
+		.mode = MODE_HIGHLIGHT
+	}, 
+
+	.helpcmd = {
+		.fg = FG_LIGHTBLUE, 
+		.bg = BG_BLACK, 
+		.mode = MODE_HIGHLIGHT
+	}, 
+
+	.helpmsg = {
+		.fg = FG_WHITE, 
+		.bg = BG_BLACK, 
+		.mode = MODE_LINE
 	}
 };
 
@@ -59,6 +77,9 @@ struct theme *current_theme = &theme_def;
 #define curr_sysusername 	(&current_theme->sysusername)
 #define curr_msg 		(&current_theme->msg)
 #define curr_sysmsg 		(&current_theme->sysmsg)
+#define curr_errmsg 		(&current_theme->errmsg)
+#define curr_helpcmd 		(&current_theme->helpcmd)
+#define curr_helpmsg 		(&current_theme->helpmsg)
 
 
 int pretty_new_msg(char *usrname, char *buf) {
@@ -105,3 +126,27 @@ int pretty_prompt(char *usrname) {
 	return 0;
 }
 
+
+int pretty_unknow_cmd(char *s) {
+	setcolor(stderr, curr_errmsg);
+	fputs(s, stderr);
+	ansi_csi_ed(stderr);
+
+	return 0;
+}
+
+
+int pretty_cmd_help(char *cmdname, char *descr) {
+	setcolor(stdout, curr_helpcmd);
+	printf("%s: ", cmdname);
+
+	setcolor(stdout, curr_helpmsg);
+	printf("%s\n", descr);
+
+	ansi_csi_ed(stdout);
+
+	return 0;
+}
+
+
+

+ 15 - 0
vcc.c

@@ -61,3 +61,18 @@ int read_msg(struct vcc_request *buf) {
 }
 
 
+int get_users(void) {
+	struct vcc_request req;
+
+	req.magic = htonl(VCC_MAGIC);
+	req.reqtype = htonl(REQ_CTL_USRS);
+	req.uid = 0;
+
+	write(fd, &req, REQ_SIZE);
+
+	/* read in another task */
+
+	return 0;
+}
+
+