Added "vbtest" for simple library testing, command mode is implemented.
[vblib.git] / vbtest.c
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);
+}