Added "vbtest" for simple library testing, command mode is implemented.
authorshort <>
Thu, 17 Feb 2000 16:37:09 +0000 (16:37 +0000)
committershort <>
Thu, 17 Feb 2000 16:37:09 +0000 (16:37 +0000)
First compilable version.
attribute((weak)) used and required, autocheck TODO.

Makefile
vblib.c
vblib.h
vbtest.c [new file with mode: 0644]

index 2a0c8c8..87cf99a 100644 (file)
--- 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 (file)
--- 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;i<NELEM(chat_cmdint);) {
                if (cmdt->name) cmd=cmdt++;
                else cmd=&chat_cmdint[i++];
diff --git a/vblib.h b/vblib.h
index 0b3e93c..c40f47d 100644 (file)
--- a/vblib.h
+++ b/vblib.h
  ****************************/
 
 #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: "
 #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 (file)
index 0000000..429761e
--- /dev/null
+++ b/vbtest.c
@@ -0,0 +1,69 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+
+#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<argc;arg++)
+               printf("Ignored argument %d: %s\n",arg,*++argv);
+       VB_LOG(CRIT,"Trying CRIT dumb logging for the %dst time",1);
+       vbinit(&vbin);
+       vbinit(&vbout); chat_vbout=&vbout;
+       while (!closing) {
+               fputs("> ",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<offs+1)
+                       buf=realloc(buf,min(2*(offs+1),1024));
+               vbread(&vbin,buf,offs+1);
+               buf[offs]='\0';
+               chat_proccmd("vbtest",buf);
+               vbreadfd(&vbout,STDOUT_FILENO);
+               }
+       puts("Closed.");
+       return(EXIT_SUCCESS);
+}