Sfoglia il codice sorgente

vcc: support gnu m4, -m4def and a multi-line command -ml

intirain 4 anni fa
parent
commit
7e16582fc6
12 ha cambiato i file con 278 aggiunte e 93 eliminazioni
  1. 2 2
      Makefile
  2. 34 0
      cmd.c
  3. 10 0
      include/vcc/plugin.h
  4. 2 2
      include/vcc/version.h
  5. 25 6
      main.c
  6. 65 3
      plugins.c
  7. 1 1
      plugins/Makefile
  8. 0 75
      plugins/lcn/lcn.c
  9. 2 2
      plugins/m4/Makefile
  10. 137 0
      plugins/m4/m4.c
  11. 0 1
      plugins/robot/robot.c
  12. 0 1
      robot.c

+ 2 - 2
Makefile

@@ -1,6 +1,6 @@
 VCC_MAJOR 		= 1
-VCC_MINOR 		= 14
-VCC_PATCH 		= 1
+VCC_MINOR 		= 15
+VCC_PATCH 		= 0
 VCC_STR 		= -rc 
 
 VCC_COMPILE_TIME 	= $(shell date)

+ 34 - 0
cmd.c

@@ -59,7 +59,10 @@ int do_cmd_unban(char *);
 int do_cmd_encry(char *);
 int do_cmd_pls(char *);
 int do_cmd_pins(char *);
+int do_cmd_prem(char *);
+int do_cmd_ml(char *);
 
+int handle_command(char *);
 int read_msg(struct vcc_request *);
 int vcc_create_session(char *name);
 int get_user_info(char *usrname);
@@ -97,6 +100,8 @@ struct cmdtable cmdtab[] = {
 	{ "-encry", do_cmd_encry, "Send an encrypted message. " }, 
 	{ "-pls", do_cmd_pls, "List plugins installed (not inserted). " }, 
 	{ "-pins", do_cmd_pins, "Insert a plugin. " }, 
+	{ "-prem", do_cmd_prem, "Remove a plugin. " }, 
+	{ "-ml", do_cmd_ml, "Multi-line. " }, 
 
 	{ NULL, NULL, NULL }
 };
@@ -498,3 +503,32 @@ int do_cmd_encry(char *s) {
 	return 0;
 }
 
+int do_cmd_ml(char *s) {
+	char 	*p;
+	int 	i, size;
+
+	(void) 	s;
+
+	if (unlikely(!(p = malloc(MSG_SIZE)))) {
+		fprintf(stderr, "*** malloc() failed\n");
+
+		return 1;
+	}
+
+	printf("Type 'finish' at the starting of a line to finish. \n");
+
+	for (i = 0; i < (int) MSG_SIZE; i += size) {
+		size = read(0, p + i, 128);
+
+		if (!strncmp(p + i, "finish", 6)) 
+			break;
+	}
+
+	p[i] = 0;
+	handle_command(p);
+	free(p);
+
+	return 0;
+}
+
+

+ 10 - 0
include/vcc/plugin.h

@@ -28,6 +28,10 @@
 /* a plugin can define their own commands in cmd.c, and must check 
  * it inserted, and abort command execution if it doesn't. */
 
+
+typedef char *(* hookfn_t) (char *);
+extern hookfn_t msg_hook;
+
 struct cmdtable {
 	char 			*cmd;
 	int 			(* do_cmd) (char *);
@@ -41,6 +45,10 @@ struct plugin {
 	char 			*name;
 	/* short description */
 	char 			*descr;
+	int 			(* init) (void);
+	int 			(* exit) (void);
+	
+	hookfn_t 		hook;
 	struct cmdtable 	*cmdtab;
 };
 
@@ -57,6 +65,8 @@ extern struct plugin *plugins[];
 
 int plg_insert(struct plugin *);
 struct plugin *find_plg(char *name);
+int register_hook(hookfn_t, struct plugin *);
+int unregister_hook(hookfn_t, struct plugin *);
 
 #endif
 

+ 2 - 2
include/vcc/version.h

@@ -1,2 +1,2 @@
-#define VCC_VERSION "VCC 1.14.1 Sun May  1 14:39:46 CST 2022 root"
-#define VCC_SEMVER "vcc 1.14.1-rc "
+#define VCC_VERSION "VCC 1.15.0 Tue May  3 16:17:18 CST 2022 root"
+#define VCC_SEMVER "vcc 1.15.0-rc "

+ 25 - 6
main.c

@@ -40,6 +40,7 @@
 #include <vcc/vcc.h>
 #include <vcc/version.h>
 #include <vcc/robot.h>
+#include <vcc/plugin.h>
 #include <vcc/pretty.h>
 #include <vcc/defserv.h> 
 
@@ -47,9 +48,6 @@
 /* we tried to use libreadline, but after using that, we feel strange 
  * because... because of what? i don't know. */
 
-#define DEFAULT_RATE 	1000
-
-
 int send_msg(const char *msg, const char *usrname);
 int read_msg(struct vcc_request *buf);
 int get_msgs(void);
@@ -235,15 +233,36 @@ int parse_args(int argc, char **argv, int *port, char **ip) {
 	return got_usrname;
 }
 
-int do_handle_command(char *buf) {
-	if (*buf == '-') 
+
+int handle_command(char *buf) {
+	switch (*buf) {
+	case '\n':
+		break;
+
+	case '-':
 		do_cmd(buf);
 
-	else if (*buf != '\n') {
+		break;
+	
+	default:
+		if (msg_hook) 
+			if (unlikely(!(buf = msg_hook(buf)))) 
+				return 1;
+
 		send_msg(buf, usrname);
 		pretty_new_msg(usrname, buf, current_sid);
+
+		if (msg_hook) 
+			free(buf);
+
+		break;
 	}
 
+	return 0;
+}
+
+int do_handle_command(char *buf) {
+	handle_command(buf);
 	pretty_prompt(usrname);
 
 	return 0;

+ 65 - 3
plugins.c

@@ -42,10 +42,11 @@
 #include <vcc/pretty.h>
 
 
-extern struct plugin plg_hello, plg_robot, plg_lcn;
+extern struct plugin plg_hello, plg_robot, plg_m4;
+hookfn_t msg_hook;
 
 struct plugin *plugins[] = {
-	&plg_hello, &plg_robot, &plg_lcn, 
+	&plg_hello, &plg_robot, &plg_m4, 
 	(struct plugin *) NULL
 };
 
@@ -79,6 +80,41 @@ int plg_insert(struct plugin *plg) {
 	return 0;
 }
 
+int plg_remove(struct plugin *plg) {
+	plg->state = 0;
+
+	return 0;
+}
+
+
+int do_cmd_prem(char *s) {
+	struct plugin *p;
+	char name[MSG_SIZE];
+
+	(void) s;
+
+	fprintf(stderr, "plugin: ");
+	scanf("%s", name);
+
+	if (!(p = find_plg(name))) {
+		fprintf(stderr, "*** plugin not found. \n");
+
+		return 1;
+	}
+
+	if (p->state != PLG_INSERTED) {
+		fprintf(stderr, "*** plugin didn't insert. \n");
+
+		return 1;
+	}
+
+	if (p->exit) 
+		p->exit();
+	
+	return plg_remove(p);
+}
+
+
 int do_cmd_pins(char *s) {
 	struct plugin *p;
 	char name[MSG_SIZE];
@@ -94,7 +130,33 @@ int do_cmd_pins(char *s) {
 		return 1;
 	}
 	
-	return plg_insert(p);
+	plg_insert(p);
+	
+	if (p->init) 
+		p->init();
+
+	return 0;
+}
+
+
+int register_hook(hookfn_t hook, struct plugin *plg) {
+	/* unlike other things, we can have only one hook at the same moment. */
+
+	if (msg_hook) 
+		fprintf(stderr, "warning: the old hook removed. \n");
+		
+	plg->hook = msg_hook = hook;
+	return 0;
+}
+
+
+int unregister_hook(hookfn_t hook, struct plugin *plg) {
+	if (msg_hook != hook) 
+		return 0;
+
+	plg->hook = msg_hook = NULL;
+
+	return 0;
 }
 
 

+ 1 - 1
plugins/Makefile

@@ -1,4 +1,4 @@
-plugins 	= hello robot lcn 
+plugins 	= hello robot m4 
 target 		= libplugins.a 
 
 all: 

+ 0 - 75
plugins/lcn/lcn.c

@@ -1,75 +0,0 @@
-/* vcc/plugins/robot/lcn.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 <string.h>
-#include <dirent.h>
-#include <arpa/inet.h>
-
-#include <vcc/vcc.h>
-#include <vcc/pretty.h>
-#include <vcc/plugin.h>
-#include <vcc/version.h>
-
-
-#define LCN_PATH 	"/root/lcn-articles/"
-
-int send_msg(char *, char *);
-
-struct cmdtable lcn_cmdtab[] = {
-	{ NULL, 0, 0 }
-};
-
-struct plugin plg_lcn = {
-	.state = 0, 
-	.name = "lcn", 
-	.descr = "The lcn plugin: read lcn articles online. ", 
-	.cmdtab = lcn_cmdtab
-};
-
-#define this_plg (&plg_lcn)
-
-int robot_lslcn(char *s) {
-	DIR *dir;
-	struct dirent *dirent;
-	char p[MSG_SIZE];
-
-	(void) s;
-
-	if (!(dir = opendir(LCN_PATH))) {
-		send_msg("Oops! There are something wrong when I tried to finish my work...\n", "robot");
-
-		return 1;
-	}
-
-	while ((dirent = readdir(dir))) {
-		if (dirent->d_type == DT_REG) {
-			memset(p, 0, MSG_SIZE);
-			sprintf(p, "%s\n", dirent->d_name);
-			send_msg(p, "robot");
-		}
-	}
-
-	closedir(dir);
-	
-	return 0;
-}
-

+ 2 - 2
plugins/lcn/Makefile → plugins/m4/Makefile

@@ -1,5 +1,5 @@
-objs 	= lcn.o 
-target 	= ../plg_lcn.o 
+objs 	= m4.o 
+target 	= ../plg_m4.o 
 CFLAGS 	= -std=gnu11 -Wall -Wextra -Werror -I../../include -g 
 
 $(target): $(objs)

+ 137 - 0
plugins/m4/m4.c

@@ -0,0 +1,137 @@
+/* vcc/plugins/m4/m4.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 <unistd.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/wait.h>
+
+#include <vcc/vcc.h>
+#include <vcc/pretty.h>
+#include <vcc/plugin.h>
+#include <vcc/version.h>
+
+
+int m4_init(void);
+int do_cmd_m4def(char *s);
+
+char *m4cachep;
+int m4cache_size;
+
+struct cmdtable m4_cmdtab[] = {
+	{ "-m4def", do_cmd_m4def, "Define a macro which i will remember. " }, 
+	{ NULL, NULL, NULL }
+};
+
+struct plugin plg_m4 = {
+	.state = 0, 
+	.name = "m4", 
+	.descr = "GNU M4 Expanding. ", 
+	.init = m4_init, 
+	/* the hook will remove by plugins.c */
+	.cmdtab = m4_cmdtab
+};
+
+#define this_plg (&plg_m4)
+
+int do_cmd_m4def(char *s) {
+	char 	buf[MSG_SIZE];
+	int 	size, i = 0, t;
+
+	(void) 	s;
+
+	plg_check_enabled();
+
+	memset(buf, 0, MSG_SIZE);
+
+	for (i = 0; i < (int) MSG_SIZE; i += t) {
+		fprintf(stderr, "m4> ");
+		t = read(0, buf + i, MSG_SIZE);
+
+		if (!strncmp(buf + i, "finish", 6)) 
+			break;
+
+		size = t;
+
+		/* remember it */
+		if (unlikely(!(m4cachep = realloc(m4cachep, m4cache_size + size)))) {
+			fprintf(stderr, "*** malloc() failed\n");
+
+			return 1;
+		}
+
+		memset(m4cachep + m4cache_size, 0, size);
+
+		strcat(m4cachep, buf + i);
+		m4cache_size += size;
+	}
+
+	printf("%s\n", m4cachep);
+
+	return 0;
+}
+
+char *m4_msghook(char *s) {
+	int 	fdt, size;
+	FILE 	*fp;
+	char 	*p;
+
+	if (unlikely((fdt = open("/tmp/.vcc-tmp", O_WRONLY | O_TRUNC | O_CREAT, 0666)) < 0)) {
+		perror("open");
+
+		return NULL;
+	}
+	
+	write(fdt, m4cachep, m4cache_size);
+
+	size = strlen(s) + 1;
+	write(fdt, s, size);
+	close(fdt);
+
+	if (unlikely(!(fp = popen("/bin/m4 /tmp/.vcc-tmp", "r")))) {
+		perror("popen");
+
+		return NULL;
+	}
+
+	if (unlikely(!(p = malloc(size)))) {
+		fprintf(stderr, "*** malloc() failed\n");
+
+		return NULL;
+	}
+
+	memset(p, 0, size);
+	fread(p, 1, size << 2, fp);
+
+	return p;
+}
+
+
+int m4_init(void) {
+	m4cachep = NULL;
+	m4cache_size = 0;
+
+	return register_hook(m4_msghook, this_plg);
+}
+
+

+ 0 - 1
plugins/robot/robot.c

@@ -46,7 +46,6 @@ struct plugin plg_robot = {
 };
 
 static struct cmdtable commands[] = {
-	{ "lslcn", robot_lslcn, "List articles of LCTT. " }, 
 	{ NULL, NULL, NULL }
 };
 

+ 0 - 1
robot.c

@@ -93,7 +93,6 @@ int robot(void) {
 	struct vcc_request req;
 
 	plg_insert(find_plg("robot"));
-	plg_insert(find_plg("lcn"));
 //	becomes_daemon();
 
 	vcc_login("robot", "robot");