Quellcode durchsuchen

vcc: add history

intirain vor 4 Jahren
Ursprung
Commit
bd41451998
9 geänderte Dateien mit 131 neuen und 16 gelöschten Zeilen
  1. 7 5
      include/vcc/plugin.h
  2. 1 1
      include/vcc/version.h
  3. 3 2
      libs/Makefile
  4. BIN
      libs/libreadconf.a
  5. 2 2
      main.c
  6. 6 5
      plugins.c
  7. 1 1
      plugins/Makefile
  8. 14 0
      plugins/hist/Makefile
  9. 97 0
      plugins/hist/hist.c

+ 7 - 5
include/vcc/plugin.h

@@ -31,7 +31,9 @@
 
 
 typedef char *(* hookfn_t) (char *);
-extern hookfn_t msg_hook, recv_hook;
+typedef char *(* recvhook_t) (char *, char *);
+extern hookfn_t msg_hook;
+extern recvhook_t recv_hook;
 
 struct cmdtable {
 	char 			*cmd;
@@ -49,8 +51,8 @@ struct plugin {
 	int 			(* init) (void);
 	int 			(* exit) (void);
 	
-	hookfn_t 		msg_hook, 
-				recv_hook;
+	hookfn_t 		msg_hook;
+	recvhook_t 		recv_hook;
 	struct cmdtable 	*cmdtab;
 };
 
@@ -71,8 +73,8 @@ int plg_insert(struct plugin *);
 struct plugin *find_plg(char *name);
 int register_msghook(hookfn_t, struct plugin *);
 int unregister_msghook(hookfn_t, struct plugin *);
-int register_recvhook(hookfn_t, struct plugin *);
-int unregister_recvhook(hookfn_t, struct plugin *);
+int register_recvhook(recvhook_t, struct plugin *);
+int unregister_recvhook(recvhook_t, struct plugin *);
 
 #endif
 

+ 1 - 1
include/vcc/version.h

@@ -1,2 +1,2 @@
-#define VCC_VERSION "VCC 1.25.0 Sat Jun  4 07:05:49 PM CST 2022 root"
+#define VCC_VERSION "VCC 1.25.0 Sun Jun  5 09:00:36 PM CST 2022 root"
 #define VCC_SEMVER "vcc 1.25.0-rc "

+ 3 - 2
libs/Makefile

@@ -7,7 +7,8 @@ all :
 
 .PHONY : clean
 clean : 
-	@for i in $(libs); do \
+	for i in $(libs); do \
 		$(MAKE) -C $$i clean; \
-	done;	
+		rm -f lib$$i.a; \
+	done;
 	

BIN
libs/libreadconf.a


+ 2 - 2
main.c

@@ -275,7 +275,7 @@ int new_msg(char *msg, char *name, int session, int flags) {
 		return 1;
 
 	if (recv_hook) 
-		if (unlikely(!(p = recv_hook(msg)))) 
+		if (unlikely(!(p = recv_hook(msg, name)))) 
 			return 1;
 
 	pretty_new_msg(name, msg, session, flags);
@@ -350,8 +350,8 @@ int main(int argc, char **argv) {
 	fds[1].events = POLLIN;
 	fds[1].revents = 0;
 
-	init_signal();
 	pretty_init();
+	init_signal();
 
 	if (unlikely(!(buf = malloc(PATH_MAX)))) {
 		fprintf(stderr, "*** malloc() failed\n");

+ 6 - 5
plugins.c

@@ -43,11 +43,12 @@
 #include <vcc/interfaces.h>
 
 
-extern struct plugin plg_hello, plg_robot, plg_m4, plg_script;
-hookfn_t msg_hook, recv_hook;
+extern struct plugin plg_hello, plg_robot, plg_m4, plg_script, plg_hist;
+hookfn_t msg_hook;
+recvhook_t recv_hook;
 
 struct plugin *plugins[] = {
-	&plg_hello, &plg_robot, &plg_m4, &plg_script, 
+	&plg_hello, &plg_robot, &plg_m4, &plg_script, &plg_hist, 
 	(struct plugin *) NULL
 };
 
@@ -187,7 +188,7 @@ int register_msghook(hookfn_t hook, struct plugin *plg) {
 }
 
 
-int register_recvhook(hookfn_t hook, struct plugin *plg) {	
+int register_recvhook(recvhook_t hook, struct plugin *plg) {	
 	if (recv_hook) 
 		fprintf(stderr, _("warning: the old hook removed. \n"));
 		
@@ -206,7 +207,7 @@ int unregister_msghook(hookfn_t hook, struct plugin *plg) {
 	return 0;
 }
 
-int unregister_recvhook(hookfn_t hook, struct plugin *plg) {
+int unregister_recvhook(recvhook_t hook, struct plugin *plg) {
 	if (recv_hook != hook) 
 		return 0;
 

+ 1 - 1
plugins/Makefile

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

+ 14 - 0
plugins/hist/Makefile

@@ -0,0 +1,14 @@
+objs 	= hist.o 
+target 	= ../plg_hist.o 
+CFLAGS 	= -std=gnu11 -Wall -Wextra -Werror -I../../include -g -I../../libs/readconf/include 
+
+$(target): $(objs)
+	@cp $(objs) $(target)
+
+%.o: %.c
+	@$(CC) -c $*.c $(CFLAGS)
+	@echo -e "CC\t\t\t$*.c"
+
+.PHONY clean: 
+	rm -f $(target) $(objs)
+

+ 97 - 0
plugins/hist/hist.c

@@ -0,0 +1,97 @@
+/* vcc/plugins/hist/hist.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 <string.h>
+#include <limits.h>
+
+#include <sys/types.h>
+#include <sys/times.h>
+
+#include <vcc/vcc.h>
+#include <vcc/pretty.h>
+#include <vcc/plugin.h>
+#include <vcc/version.h>
+
+#include <readconf/readconf.h>
+
+
+int hist_init(void);
+int hist_fd;
+
+struct cmdtable hist_cmdtab[] = {
+	{ NULL, NULL, NULL }
+};
+
+struct plugin plg_hist = {
+	.state = 0, 
+	.name = "hist", 
+	.descr = "Show history. ", 
+	.init =  hist_init, 
+	.cmdtab = hist_cmdtab
+};
+
+#define this_plg (&plg_hist)
+#define DEFAULT_HISTFILE ".vcc_history"
+
+char *hist_recvhook(char *s, char *usr) {
+	dprintf(hist_fd, "[%ld] %s: %s", times(NULL), usr, s);
+	return strdup(s);
+}
+
+
+int open_hist_file(void) {
+	struct readconf_strconf *conf;
+	char *p;
+	char buf[PATH_MAX];
+
+	if (!(conf = readconf_find_strname("histfile"))) {
+		p = getenv("HOME");
+		sprintf(buf, "%s/" DEFAULT_HISTFILE, p);
+		p = buf;
+	}
+	
+	else 
+		p = conf->conf_value;
+
+	printf("using history file %s\n", p);
+
+	if (unlikely((hist_fd = open(p, O_WRONLY | O_APPEND | O_CREAT)) < 0)) {
+		perror("open()");
+		return -1;
+	}
+
+	return hist_fd;
+}
+
+int hist_init(void) {
+	if (unlikely((hist_fd = open_hist_file()) < 0)) {
+		fprintf(stderr, "*** unable to open history file. \n");
+
+		return 1;
+	}
+
+	return register_recvhook(hist_recvhook, this_plg);
+}
+