From 544c0e8fdbff6f5e4a33026711a0f847441a8118 Mon Sep 17 00:00:00 2001 From: short <> Date: Thu, 17 Feb 2000 16:37:09 +0000 Subject: [PATCH] Added "vbtest" for simple library testing, command mode is implemented. First compilable version. attribute((weak)) used and required, autocheck TODO. --- Makefile | 15 ++++++++++---- vblib.c | 15 ++++++++++++-- vblib.h | 8 ++++++-- vbtest.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 99 insertions(+), 8 deletions(-) create mode 100644 vbtest.c diff --git a/Makefile b/Makefile index 2a0c8c8..87cf99a 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,17 @@ -CFLAGS=-Wall -Wstrict-prototypes -O2 +CFLAGS=-Wall -Wstrict-prototypes -O2 -ggdb +TARGETS=vbtest -all: vblib.o +all: $(TARGETS) + +vbtest: vbtest.o vblib.o + $(LINK.o) $(OUTPUT_OPTION) $^ + +vbtest.o: vbtest.c vblib.h + $(COMPILE.c) $(OUTPUT_OPTION) $< vblib.o: vblib.c vblib.h - $(CC) $(CFLAGS) -o $@ $< + $(COMPILE.c) $(OUTPUT_OPTION) $< .PHONY: clean clean: - $(RM) *.o + $(RM) *.o $(TARGETS) diff --git a/vblib.c b/vblib.c index 9bf8ea1..8850dc1 100644 --- a/vblib.c +++ b/vblib.c @@ -801,14 +801,21 @@ char *chat_cmdpars[CHAT_CMD_MAXPARS]; static void chatfn_help(void); +void chatfn_quit(void) __WEAK; +void chatfn_quit(void) +{ + VB_LOG(CRIT,"chatfn_quit() not initialized"); +} + static const struct chat_cmdtab chat_cmdint[]={ {"help" ,0,chatfn_help ,"This help"}, -#if 0 {"quit" ,0,chatfn_quit ,"Close connection"}, {"exit" ,0,chatfn_quit ,"Close connection"}, -#endif }; +const struct chat_cmdtab chat_cmdtable[] __WEAK = + {{NULL,0,NULL,NULL}}; + static void chatfn_help(void) { int i; @@ -858,6 +865,10 @@ const struct chat_cmdtab *cmd,*cmdt; } chat_parsepars(s); cmdt=chat_cmdtable; + if (!cmdt || !cmdt->name) { + VB_LOG(CRIT,"chat_cmdtable not initialized, \"%s\" tried to execute \"%s\"",who,cmds); + return; + } for (i=0;iname) cmd=cmdt++; else cmd=&chat_cmdint[i++]; diff --git a/vblib.h b/vblib.h index 0b3e93c..c40f47d 100644 --- a/vblib.h +++ b/vblib.h @@ -69,10 +69,10 @@ ****************************/ #ifndef VB_LOG -#define VB_LOG(lev,msg...) fprintf(stderr,VB_LOG_##lev ##msg) +#define VB_LOG(lev,msg,args...) fprintf(stderr,VB_LOG_##lev msg "\n", ##args) #endif #ifndef VB_FATAL -#define VB_FATAL(lev,msg...) do { VB_LOG(lev, ##msg); exit(EXIT_FAILURE); } while (0) +#define VB_FATAL(lev,msg,args...) do { VB_LOG(lev,msg , ##args); exit(EXIT_FAILURE); } while (0) #endif #define VB_LOG_DEBUG "VB_Debug: " @@ -98,6 +98,10 @@ #ifndef __NORETURN #define __NORETURN __attribute__((__noreturn__)) #endif +/* for chat_cmdtable: */ +#ifndef __WEAK +#define __WEAK __attribute__((__weak__)) +#endif #define NELEM(a) (sizeof((a))/sizeof(*(a))) #define XRAWDIGITI(n) ((n)-'0'-((n)>='A'?'A'-('9'+1):0)) diff --git a/vbtest.c b/vbtest.c new file mode 100644 index 0000000..429761e --- /dev/null +++ b/vbtest.c @@ -0,0 +1,69 @@ +#include +#include +#include +#include + +#include "vblib.h" + +static struct varbuf vbin,vbout; +static int closing=0; + +void chatfn_quit(void) +{ + vbputstring(&vbout,"Closing...\n"); + closing=1; +} + +void chatfn_test0(void) +{ +static int total=0; + + vbprintf(&vbout,"Test-0 command run times: %d\n",total++); +} + +void chatfn_test2(void) +{ +static int total=0; + + vbprintf(&vbout,"Test-2 command run times: %d, arg0=\"%s\", arg1=\"%s\"\n", + total++,chat_cmdpars[0],chat_cmdpars[1]); +} + +const struct chat_cmdtab chat_cmdtable[]={ + {"test0",0,chatfn_test0,"Test command with no arguments"}, + {"test2",2,chatfn_test2,"Test command with 2 arguments"}, + {NULL,0,NULL,NULL}}; + +int main(int argc,char **argv) +{ +int arg; +char *buf=NULL; +size_t bufsiz=0; +ssize_t offs,now; + + for (arg=1;arg ",stdout); fflush(stdout); + while ((offs=vbchrn(&vbin,0,'\n',+1))==-1) { + if (!vbin.free) vbgrow(&vbin,0); + now=read(STDIN_FILENO,vbin.l->buf+vbin.l->size-vbin.free,vbin.free); + if (now<=0) { + printf("\nError while reading input: %s\n",strerror(errno)); + exit(EXIT_FAILURE); + } + vbin.free-=now; + } + if (bufsiz