Added very primitive documentation files.
[vblib.git] / vbtest.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <errno.h>
5
6 #include "vblib.h"
7
8 static struct varbuf vbin,vbout;
9 static int closing=0;
10
11 void chatfn_quit(void)
12 {
13         vbputstring(&vbout,"Closing...\n");
14         closing=1;
15 }
16
17 void chatfn_test0(void)
18 {
19 static int total=0;
20
21         vbprintf(&vbout,"Test-0 command run times: %d\n",total++);
22 }
23
24 void chatfn_test2(void)
25 {
26 static int total=0;
27
28         vbprintf(&vbout,"Test-2 command run times: %d, arg0=\"%s\", arg1=\"%s\"\n",
29                 total++,chat_cmdpars[0],chat_cmdpars[1]);
30 }
31
32 const struct chat_cmdtab chat_cmdtable[]={
33         {"test0",0,chatfn_test0,"Test command with no arguments"},
34         {"test2",2,chatfn_test2,"Test command with 2 arguments"},
35         {NULL,0,NULL,NULL}};
36
37 int main(int argc,char **argv)
38 {
39 int arg;
40 char *buf=NULL;
41 size_t bufsiz=0;
42 ssize_t offs,now;
43
44         for (arg=1;arg<argc;arg++)
45                 printf("Ignored argument %d: %s\n",arg,*++argv);
46         VB_LOG(CRIT,"Trying CRIT dumb logging for the %dst time",1);
47         vbinit(&vbin);
48         vbinit(&vbout); chat_vbout=&vbout;
49         while (!closing) {
50                 fputs("> ",stdout); fflush(stdout);
51                 while ((offs=vbchrn(&vbin,0,'\n',+1))==-1) {
52                         if (!vbin.free) vbgrow(&vbin,0);
53                         now=read(STDIN_FILENO,vbin.l->buf+vbin.l->size-vbin.free,vbin.free);
54                         if (now<=0) {
55                                 printf("\nError while reading input: %s\n",strerror(errno));
56                                 exit(EXIT_FAILURE);
57                                 }
58                         vbin.free-=now;
59                         }
60                 if (bufsiz<offs+1)
61                         buf=realloc(buf,min(2*(offs+1),1024));
62                 vbread(&vbin,buf,offs+1);
63                 buf[offs]='\0';
64                 chat_proccmd("vbtest",buf);
65                 vbreadfd(&vbout,STDOUT_FILENO);
66                 }
67         puts("Closed.");
68         return(EXIT_SUCCESS);
69 }