Browse code

Compilation fixes. Implementation of loglib and parselib

Dario Rodriguez authored on 14/03/2014 12:42:03
Showing 8 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,28 @@
1
+CC=gcc
2
+CFLAGS=-g -Wall
3
+LDFLAGS=
4
+
5
+all: webkernel_test
6
+
7
+clean:
8
+	rm -f *.o webkernel_test
9
+
10
+webkernel_test: webkernel_test.o iobuf.o loglib.o parselib.o sbuf.o \
11
+		socklib.o webkernel.o
12
+	$(CC) $(LDFLAGS) webkernel_test.o iobuf.o loglib.o parselib.o sbuf.o \
13
+		socklib.o webkernel.o -o webkernel_test
14
+
15
+webkernel_test.o:
16
+
17
+iobuf.o:
18
+
19
+loglib.o:
20
+
21
+parselib.o:
22
+
23
+sbuf.o:
24
+
25
+socklib.o:
26
+
27
+webkernel.o:
28
+
... ...
@@ -5,12 +5,14 @@
5 5
  *
6 6
  * History:
7 7
  *      12/03/2014 Creation.
8
+ *      14/03/2014 Compilation fixes.
8 9
  *
9 10
  * Author: Dario Rodriguez dario@softhome.net
10 11
  * This library is licensed on the terms of the GNU LGPL v2+
11 12
  */
12 13
 
13 14
 #include <stdlib.h>
15
+#include <string.h>
14 16
 #include "iobuf.h"
15 17
 
16 18
 /* exported funcions */
... ...
@@ -37,9 +39,9 @@ siobuf *
37 39
 iobuf_staticinit(siobuf *uninitiobuf, sbuf *uninitinbuf, sbuf *uninitoutbuf,int insize, int outsize, char *inbuf, char *outbuf)
38 40
 {
39 41
         memset(uninitiobuf,0,sizeof(siobuf));
40
-        if((siobuf->in=sbuf_staticinit(uninitinbuf,insize,inbuf))==NULL)
42
+        if((uninitiobuf->in=sbuf_staticinit(uninitinbuf,insize,inbuf))==NULL)
41 43
                 return(NULL);
42
-        if((siobuf->out=sbuf_staticinit(uninitoutbuf,outsize,outbuf))==NULL)
44
+        if((uninitiobuf->out=sbuf_staticinit(uninitoutbuf,outsize,outbuf))==NULL)
43 45
                 return(NULL);
44 46
         return(uninitiobuf);
45 47
 }
... ...
@@ -66,5 +68,4 @@ iobuf_staticfree(siobuf *iobuf)
66 68
         return;
67 69
 }
68 70
 
69
-#endif
70 71
 
71 72
new file mode 100644
... ...
@@ -0,0 +1,139 @@
1
+/*
2
+ * loglib.h
3
+ *
4
+ * A non-intrusive minimal log framework.
5
+ *
6
+ * History:
7
+ *      14/03/2014 Creation
8
+ *
9
+ * Author: Dario Rodriguez dario@softhome.net
10
+ * This file is licensed under the terms of the GNU LGPL v2+
11
+ */
12
+
13
+#include <stdio.h>
14
+#include <stdlib.h>
15
+#include <unistd.h>
16
+#include <string.h>
17
+#include <time.h>
18
+#include <stdarg.h>
19
+#include <sys/types.h>
20
+#include <sys/stat.h>
21
+#include <fcntl.h>
22
+
23
+#include "loglib.h"
24
+
25
+#define MAXLOGFILENAME 1024
26
+#define LOGWRITEBUFSIZE 4096
27
+#define LOGWRITEHEXDUMPSIZE 4096
28
+
29
+#define DEFAULTFD 2
30
+
31
+void
32
+log_write(char *type, char *format, ...)
33
+{
34
+        static void (*writefunc)(int fd, char *buf, long bufsize, void *usrptr)=NULL;
35
+        static void *usrptr=NULL;
36
+        static int fd=DEFAULTFD;
37
+        static char logfilename[MAXLOGFILENAME]={""};
38
+        static char writebuf[LOGWRITEBUFSIZE];
39
+        char *ptr;
40
+        time_t t;
41
+        struct tm stm;
42
+        va_list arglist;
43
+        /* if it is a special action, perform it */
44
+        if(type==((char *)log_setlogfile)) {
45
+                if(fd!=DEFAULTFD)
46
+                        close(fd),fd=DEFAULTFD;
47
+                strncpy(logfilename,format,sizeof(logfilename)-1);
48
+                logfilename[sizeof(logfilename)-1]='\0';
49
+                return;
50
+        } else if(type==((char *)log_closelogfile)) {
51
+                if(fd!=DEFAULTFD)
52
+                        close(fd),fd=DEFAULTFD;
53
+                return;
54
+        } else if(type==((char *)log_setwritefunc)) {
55
+                writefunc=(void (*)(int /*fd*/, char * /*buf*/, long /*bufsize*/, void */*usrptr*/)) format;
56
+                va_start(arglist,format);
57
+                usrptr=va_arg(arglist,void *);
58
+                va_end(arglist);
59
+                return;
60
+        }
61
+        /* it is a new log entry */
62
+        /* make sure the file is open */
63
+        if(fd==DEFAULTFD && logfilename[0]!='\0') {
64
+                if((fd=open(logfilename,O_WRONLY|O_CREAT|O_APPEND,0640))==-1) {
65
+                        fd=DEFAULTFD;
66
+                        return;
67
+                }
68
+        }
69
+        /* prepare the string */
70
+        t=time(NULL);
71
+        localtime_r(&t,&stm);
72
+        ptr=writebuf;
73
+        strftime(ptr,writebuf+LOGWRITEBUFSIZE-ptr-1,"%b %e %H:%M:%S ",&stm);
74
+        writebuf[LOGWRITEBUFSIZE-1]='\0';
75
+        ptr+=strlen(ptr);
76
+        snprintf(ptr,writebuf+LOGWRITEBUFSIZE-ptr-1,"%s: ",type);
77
+        writebuf[LOGWRITEBUFSIZE-1]='\0';
78
+        ptr+=strlen(ptr);
79
+        va_start(arglist,format);
80
+        vsnprintf(ptr,writebuf+LOGWRITEBUFSIZE-ptr-1,format,arglist);
81
+        va_end(arglist);
82
+        writebuf[LOGWRITEBUFSIZE-1]='\0';
83
+        ptr+=strlen(ptr);
84
+        /* if the string is not terminated with a '\n', add the '\n' */
85
+        if(ptr>writebuf && ptr[-1]!='\n') {
86
+                if(ptr<(writebuf+LOGWRITEBUFSIZE-1)) {
87
+                        *(ptr++)='\n';
88
+                        *ptr='\0';
89
+                } else
90
+                        ptr[-1]='\n';
91
+        }
92
+        /* commit the data */
93
+        if(writefunc!=NULL)
94
+                writefunc(fd,writebuf,ptr-writebuf,usrptr);
95
+        else
96
+                write(fd,writebuf,ptr-writebuf);
97
+}
98
+
99
+char *
100
+log_hexdumpstr(char *data, long datasize)
101
+{
102
+        static char hexdumpbuf[LOGWRITEHEXDUMPSIZE];
103
+        char c,*ptr;
104
+        long i,l;
105
+        l=(datasize>(LOGWRITEHEXDUMPSIZE/3))?(LOGWRITEHEXDUMPSIZE/3):datasize;
106
+        for(i=0,ptr=hexdumpbuf;i<l;i++) {
107
+                c=data[i]&0xf;
108
+                *(ptr++)=(c<0xa)?c+'0':c+'a'-10;
109
+                c=(data[i]>>4)&0xf;
110
+                *(ptr++)=(c<0xa)?c+'0':c+'a'-10;
111
+                *(ptr++)=' ';
112
+        }
113
+        ptr[(i>0)?i-1:0]='\0';
114
+        return(hexdumpbuf);
115
+}
116
+
117
+void
118
+log_setlogfile(char *filename)
119
+{
120
+        log_write((char *)log_setlogfile,(char *)filename);
121
+}
122
+
123
+void
124
+log_closelogfile(void)
125
+{
126
+        void *dummy=NULL;
127
+        log_write((char *)log_closelogfile,(char *)dummy);
128
+}
129
+
130
+
131
+void
132
+log_setwritefunc(void (*newwritefunc)(int fd, char *buf, long bufsize, void *usrptr),void *usrptr)
133
+{
134
+        log_write((char *)log_setwritefunc,(char *)newwritefunc,usrptr);
135
+}
136
+
137
+
138
+
139
+
... ...
@@ -5,6 +5,7 @@
5 5
  *
6 6
  * History:
7 7
  *      27/01/2014 Creation
8
+ *      14/03/2014 Add log_setlogfile(), log_closelogfile.
8 9
  *
9 10
  * Author: Dario Rodriguez dario@softhome.net
10 11
  * This file is licensed under the terms of the GNU LGPL v2+
... ...
@@ -20,6 +21,9 @@ void log_write(char *type, char *format, ...) __attribute__ ((format (printf, 2,
20 21
 #endif
21 22
 
22 23
 char *log_hexdumpstr(char *data, long datasize);
24
+
25
+void log_setlogfile(char *filename);
26
+void log_closelogfile(void);
23 27
 void log_setwritefunc(void (*newwritefunc)(int fd, char *buf, long bufsize, void *usrptr),void *usrptr);
24 28
 
25 29
 #endif
26 30
new file mode 100644
... ...
@@ -0,0 +1,101 @@
1
+/*
2
+ * parselib.c
3
+ *
4
+ * Convenience functions for parsing config-file lines.
5
+ *
6
+ * History:
7
+ *      14/03/2014 Creation
8
+ *
9
+ * Author: Dario Rodriguez dario@softhome.net
10
+ * This file is licensed under the terms of the GNU LGPL v2+
11
+ */
12
+
13
+#include <string.h>
14
+#include "parselib.h"
15
+
16
+char *
17
+field_int(char *ptr, int *value)
18
+{
19
+        *value=0;
20
+        while(strchr(" \t",*ptr)!=NULL)
21
+                ptr++;
22
+        for(;*ptr>='0' && *ptr<='9';ptr++)
23
+                *value=(*value)*10+(*ptr-'0');
24
+        return(ptr);
25
+}
26
+
27
+char *
28
+field_long(char *ptr, long *value)
29
+{
30
+        *value=0;
31
+        while(strchr(" \t",*ptr)!=NULL)
32
+                ptr++;
33
+        for(;*ptr>='0' && *ptr<='9';ptr++)
34
+                *value=(*value)*10+(*ptr-'0');
35
+        return(ptr);
36
+}
37
+
38
+char *
39
+field_quotedstr(char *ptr, char *str, long strsize)
40
+{
41
+        char *dest;
42
+        int flaglit;
43
+        while(strchr(" \t",*ptr)!=NULL)
44
+                ptr++;
45
+        if(*ptr!='\"')
46
+                return(ptr);
47
+        /* skip initial '\"' */
48
+        ptr++;
49
+        /* read the string unescaping as we go */
50
+        for(dest=str,flaglit=0;*ptr!='\0' && !(!flaglit && *ptr=='\"');ptr++) {
51
+                if(flaglit==0 && *ptr=='\\') {
52
+                        flaglit=1;
53
+                        continue;
54
+                }
55
+                flaglit=0;
56
+                if(str!=NULL && (dest-str)<strsize)
57
+                        *(dest++)=*ptr;
58
+        }
59
+        if((dest-str)<strsize)
60
+                *(dest++)='\0';
61
+        else if(str!=NULL && strsize>0)
62
+                str[strsize-1]='\0';
63
+        /* skip the final '\"' */
64
+        if(*ptr=='\"')
65
+                ptr++;
66
+        return(ptr);
67
+}
68
+
69
+char *
70
+field_unquotedstr(char *ptr, char *str, long strsize, char *separators)
71
+{
72
+        char *dest;
73
+        while(strchr(" \t",*ptr)!=NULL)
74
+                ptr++;
75
+        /* read the string */
76
+        for(dest=str;*ptr!='\0' && strchr(separators,*ptr)==NULL;ptr++) {
77
+                if(str!=NULL && (dest-str)<strsize)
78
+                        *(dest++)=*ptr;
79
+        }
80
+        if((dest-str)<strsize)
81
+                *(dest++)='\0';
82
+        else if(str!=NULL && strsize>0)
83
+                str[strsize-1]='\0';
84
+        return(ptr);
85
+}
86
+
87
+char *
88
+field_hexint(char *ptr, int *value)
89
+{
90
+        *value=0;
91
+        while(strchr(" \t",*ptr)!=NULL)
92
+                ptr++;
93
+        for(;(*ptr>='0' && *ptr<='9')||(*ptr>='a' && *ptr<='f')||(*ptr>='A' && *ptr<='F');ptr++) {
94
+                *value=((*value)*16)+
95
+                (*ptr>='a' && *ptr<='f')?*ptr-'a'+10:
96
+                (*ptr>='A' && *ptr<='F')?*ptr-'A'+10:
97
+                *ptr-'0';
98
+        }
99
+        return(ptr);
100
+}
101
+
... ...
@@ -1,10 +1,13 @@
1 1
 /*
2 2
  * parselib.h
3 3
  *
4
- * Convenienct functions to parse config file lines.
4
+ * Convenience functions for parsing config-file lines.
5
+ *
6
+ * Header file.
5 7
  *
6 8
  * History:
7
- *      27/01/2014 Creation
9
+ *      27/01/2014 Creation.
10
+ *      14/03/2014 Add separators param to _unquotedstr().
8 11
  *
9 12
  * Author: Dario Rodriguez dario@softhome.net
10 13
  * This file is licensed under the terms of the GNU LGPL v2+
... ...
@@ -16,7 +19,7 @@
16 19
 char *field_int(char *ptr, int *value);
17 20
 char *field_long(char *ptr, long *value);
18 21
 char *field_quotedstr(char *ptr, char *str, long strsize);
19
-char *field_unquotedstr(char *ptr, char *str, long strsize);
22
+char *field_unquotedstr(char *ptr, char *str, long strsize, char *separators);
20 23
 char *field_hexint(char *ptr, int *value);
21 24
 
22 25
 #endif
... ...
@@ -5,6 +5,7 @@
5 5
  *
6 6
  * History:
7 7
  *      13/03/2014 Creation.
8
+ *      14/03/2014 Compilation fixes.
8 9
  *
9 10
  * Author: Dario Rodriguez dario@softhome.net
10 11
  * This library is licensed on the terms of the GNU LGPL v2+
... ...
@@ -20,7 +21,7 @@ sbuf *
20 21
 sbuf_init(long bufsize)
21 22
 {
22 23
         sbuf *buf;
23
-        if(bufsize<=0 || (buf=malloc(sizeof sbuf))==NULL)
24
+        if(bufsize<=0 || (buf=malloc(sizeof(sbuf)))==NULL)
24 25
                 return(NULL);
25 26
         memset(buf,0,sizeof(sbuf));
26 27
         if((buf->buf=malloc(bufsize))==NULL) {
... ...
@@ -50,7 +51,7 @@ sbuf_free(sbuf *buf)
50 51
         if(buf->buf!=NULL)
51 52
                 free(buf->buf),buf->buf=NULL;
52 53
         buf->size=0;
53
-        free(buf)
54
+        free(buf);
54 55
 }
55 56
 
56 57
 void
... ...
@@ -65,14 +66,14 @@ int
65 66
 sbuf_fill(sbuf *buf, int fd, long numbytes)
66 67
 {
67 68
         long n;
68
-        int read;
69
+        int nread;
69 70
         if(buf==NULL || fd==-1 || numbytes<0)
70 71
                 return(-1);
71 72
         n=(numbytes>(buf->size-buf->used))?buf->size-buf->used:numbytes;
72
-        read=read(fd,buf->buf+buf->used,n);
73
-        if(read>=0)
74
-                buf->used+=read;
75
-        return(read);
73
+        nread=read(fd,buf->buf+buf->used,n);
74
+        if(nread>=0)
75
+                buf->used+=nread;
76
+        return(nread);
76 77
 }
77 78
 
78 79
 char *
... ...
@@ -140,7 +141,7 @@ sbuf_add(sbuf *buf, char *data, long datasize)
140 141
         long added;
141 142
         if(buf==NULL || data==NULL || buf->used==buf->size)
142 143
                 return(0);
143
-        added=(datasize>(buf->size-buf->used))?(buf->size-buf->used)):datasize;
144
+        added=(datasize>(buf->size-buf->used))?(buf->size-buf->used):datasize;
144 145
         memcpy(buf->buf+buf->used,data,added);
145 146
         buf->used+=added;
146 147
         return(added);
... ...
@@ -188,8 +189,8 @@ sbuf_send(sbuf *buf, int fd, long numbytes)
188 189
         int written;
189 190
         if(buf==NULL || fd==-1 || numbytes<=0)
190 191
                 return(0);
191
-        n=(numbytes>(sbuf->used-sbuf->got))?(sbuf->used-sbuf->got):numbytes;
192
-        written=write(fd,buf->buf+buf->got,written);
192
+        n=(numbytes>(buf->used-buf->got))?(buf->used-buf->got):numbytes;
193
+        written=write(fd,buf->buf+buf->got,n);
193 194
         if(written>0)
194 195
                 buf->got+=written;
195 196
         return(written);
... ...
@@ -8,8 +8,9 @@
8 8
  * History:
9 9
  *      21/01/2014 Creation.
10 10
  *      27/01/2014 Finish initial API.
11
- *      13/03/2014 removed the low-level API, as it is redundant with
11
+ *      13/03/2014 Removed the low-level API, as it is redundant with
12 12
  *                 _staticinit()/_staticfree().
13
+ *      14/03/2014 Compilation fixes.
13 14
  *
14 15
  * Author: Dario Rodriguez dario@softhome.net
15 16
  * This library is licensed on the terms of the GNU LGPL v2+
... ...
@@ -22,7 +23,7 @@ typedef struct {
22 23
         long used;
23 24
         long got;
24 25
         long size;
25
-        char *buf
26
+        char *buf;
26 27
 } sbuf;
27 28
 
28 29
 /* sbuf, high level api */