OSSP CVS Repository

ossp - Check-in [873]
Not logged in
[Honeypot]  [Browse]  [Home]  [Login]  [Reports
[Search]  [Ticket]  [Timeline
  [Patchset]  [Tagging/Branching

Check-in Number: 873
Date: 2001-Sep-06 16:37:53 (local)
2001-Sep-06 14:37:53 (UTC)
User:rse
Branch:
Comment: Replace generic L2_ERROR with more granular L2_ERR_XXX and make sure that we always check with "!= L2_OK".
Tickets:
Inspections:
Files:
ossp-pkg/l2/TODO      1.13 -> 1.14     0 inserted, 2 deleted
ossp-pkg/l2/l2.h      1.19 -> 1.20     6 inserted, 1 deleted
ossp-pkg/l2/l2_ch_buffer.c      1.15 -> 1.16     22 inserted, 19 deleted
ossp-pkg/l2/l2_ch_file.c      1.13 -> 1.14     11 inserted, 11 deleted
ossp-pkg/l2/l2_ch_prefix.c      1.8 -> 1.9     5 inserted, 5 deleted
ossp-pkg/l2/l2_ch_socket.c      1.13 -> 1.14     20 inserted, 20 deleted
ossp-pkg/l2/l2_ch_syslog.c      1.14 -> 1.15     6 inserted, 6 deleted
ossp-pkg/l2/l2_channel.c      1.15 -> 1.16     107 inserted, 25 deleted
ossp-pkg/l2/l2_stream.c      1.13 -> 1.14     65 inserted, 22 deleted
ossp-pkg/l2/l2_ut_fmtcb.c      1.1 -> 1.2     15 inserted, 9 deleted
ossp-pkg/l2/l2_ut_format.c      1.1 -> 1.2     2 inserted, 0 deleted
ossp-pkg/l2/l2_ut_level.c      1.1 -> 1.2     3 inserted, 3 deleted
ossp-pkg/l2/l2_ut_param.c      1.3 -> 1.4     2 inserted, 2 deleted

ossp-pkg/l2/TODO 1.13 -> 1.14

--- TODO 2001/09/03 12:16:44     1.13
+++ TODO 2001/09/06 14:37:53     1.14
@@ -2,8 +2,6 @@
 OSSP L2
 =======
 
-Improvement: return L2_ERR_XXX instead of just a single L2_ERROR
-
 Idea:
 l2_handler_t:
    - function pointers


ossp-pkg/l2/l2.h 1.19 -> 1.20

--- l2.h 2001/09/06 11:56:15     1.19
+++ l2.h 2001/09/06 14:37:53     1.20
@@ -81,7 +81,12 @@
 /* list of return values */
 typedef enum {
     L2_OK,
-    L2_ERROR
+    L2_ERR_ARG,    /* invalid argument */
+    L2_ERR_USE,    /* invalid usage */
+    L2_ERR_MEM,    /* no more memory available */
+    L2_ERR_SYS,    /* system error (see errno) */
+    L2_ERR_FMT,    /* message formating error */
+    L2_ERR_INT     /* internal error */
 } l2_result_t;
 
 /* context union for storing data */


ossp-pkg/l2/l2_ch_buffer.c 1.15 -> 1.16

--- l2_ch_buffer.c       2001/09/06 11:47:51     1.15
+++ l2_ch_buffer.c       2001/09/06 14:37:53     1.16
@@ -45,7 +45,7 @@
 
     /* allocate private channel configuration */
     if ((cfg = (l2_ch_buffer_t *)malloc(sizeof(l2_ch_buffer_t))) == NULL)
-        return L2_ERROR;
+        return L2_ERR_MEM;
 
     /* initialize configuration with reasonable defaults */
     cfg->buf     = NULL;
@@ -67,14 +67,14 @@
 
     /* parameter checks */
     if ((cfg = (l2_ch_buffer_t *)ctx->vp) == NULL)
-        return L2_ERROR;
+        return L2_ERR_ARG;
 
     /* feed and call generic parameter parsing engine */
     L2_PARAM_SET(pa[0], size, INT, &cfg->bufsize);
     L2_PARAM_END(pa[1]);
     rv = l2_util_setparams(pa, fmt, ap);
     if (cfg->bufsize < 0) 
-        return L2_ERROR;
+        return L2_ERR_ARG;
     return rv;
 }
 
@@ -83,22 +83,23 @@
 {
     l2_ch_buffer_t *cfg;
     l2_channel_t *downstream;
+    l2_result_t rv;
 
     /* parameter checks */
     if ((cfg = (l2_ch_buffer_t *)ctx->vp) == NULL)
-        return L2_ERROR;
+        return L2_ERR_ARG;
 
     /* open channel buffer */
     if (cfg->bufsize > 0) {
         if ((cfg->buf = malloc(cfg->bufsize)) == NULL)
-            return L2_ERROR;
+            return L2_ERR_MEM;
         cfg->bufpos = 0;
     }
 
     /* optionally open downstream channel, too */
     if ((downstream = l2_channel_downstream(ch)) != NULL)
-        if (l2_channel_open(downstream) == L2_ERROR)
-            return L2_ERROR;
+        if ((rv = l2_channel_open(downstream)) != L2_OK)
+            return rv;
 
     return L2_OK;
 }
@@ -113,7 +114,7 @@
 
     /* parameter checks */
     if ((cfg = (l2_ch_buffer_t *)ctx->vp) == NULL)
-        return L2_ERROR;
+        return L2_ERR_ARG;
 
     if (bufsize > (cfg->bufsize - cfg->bufpos)) {
         /* flush buffer if necessary */
@@ -139,23 +140,24 @@
 {
     l2_ch_buffer_t *cfg;
     l2_channel_t *downstream;
+    l2_result_t rv;
 
     /* parameter checks */
     if ((cfg = (l2_ch_buffer_t *)ctx->vp) == NULL)
-        return L2_ERROR;
+        return L2_ERR_ARG;
 
     /* write the buffer contents downstream */
     if (cfg->bufpos > 0) {
         if ((downstream = l2_channel_downstream(ch)) != NULL)
-            if (l2_channel_write(downstream, cfg->buf, cfg->bufpos) == L2_ERROR)
-                return L2_ERROR;
+            if ((rv = l2_channel_write(downstream, cfg->buf, cfg->bufpos)) != L2_OK)
+                return rv;
         cfg->bufpos = 0;
     }
 
     /* optionally flush downstream channel, too */
     if ((downstream = l2_channel_downstream(ch)) != NULL)
-        if (l2_channel_flush(downstream) == L2_ERROR)
-            return L2_ERROR;
+        if ((rv = l2_channel_flush(downstream)) != L2_OK)
+            return rv;
 
     return L2_OK;
 }
@@ -165,23 +167,24 @@
 {
     l2_ch_buffer_t *cfg;
     l2_channel_t *downstream;
+    l2_result_t rv;
 
     /* parameter checks */
     if ((cfg = (l2_ch_buffer_t *)ctx->vp) == NULL)
-        return L2_ERROR;
+        return L2_ERR_ARG;
 
     /* write stale data before closing down */
     if (cfg->bufpos > 0) {
         if ((downstream = l2_channel_downstream(ch)) != NULL)
-            if (l2_channel_write(downstream, cfg->buf, cfg->bufpos) == L2_ERROR)
-                return L2_ERROR;
+            if ((rv = l2_channel_write(downstream, cfg->buf, cfg->bufpos)) != L2_OK)
+                return rv;
         cfg->bufpos = 0;
     }
 
     /* optionally close downstream channel, too */
     if ((downstream = l2_channel_downstream(ch)) != NULL)
-        if (l2_channel_close(downstream) == L2_ERROR)
-            return L2_ERROR;
+        if ((rv = l2_channel_close(downstream)) != L2_OK)
+            return rv;
 
     /* close channel buffer */
     if (cfg->buf != NULL) {
@@ -199,7 +202,7 @@
 
     /* parameter checks */
     if ((cfg = (l2_ch_buffer_t *)ctx->vp) == NULL)
-        return L2_ERROR;
+        return L2_ERR_ARG;
 
     /* if not already closed, close channel buffer now */
     if (cfg->buf != NULL) {


ossp-pkg/l2/l2_ch_file.c 1.13 -> 1.14

--- l2_ch_file.c 2001/09/05 07:47:12     1.13
+++ l2_ch_file.c 2001/09/06 14:37:53     1.14
@@ -48,7 +48,7 @@
 
     /* allocate private channel configuration */
     if ((cfg = (l2_ch_file_t *)malloc(sizeof(l2_ch_file_t))) == NULL)
-        return L2_ERROR;
+        return L2_ERR_ARG;
 
     /* initialize configuration with reasonable defaults */
     cfg->fd     = -1; 
@@ -71,7 +71,7 @@
 
     /* parameter checks */
     if ((cfg = (l2_ch_file_t *)ctx->vp) == NULL)
-        return L2_ERROR;
+        return L2_ERR_ARG;
 
     /* feed and call generic parameter parsing engine */
     L2_PARAM_SET(pa[0], path,   CHARPTR, &cfg->path);
@@ -91,16 +91,16 @@
 
     /* parameter checks */
     if ((cfg = (l2_ch_file_t *)ctx->vp) == NULL)
-        return L2_ERROR;
+        return L2_ERR_ARG;
     if (cfg->path == NULL)
-        return L2_ERROR;
+        return L2_ERR_USE;
 
     /* open channel file */
     mode = O_WRONLY|O_CREAT;
     if (cfg->append)
         mode |= O_APPEND;
     if ((cfg->fd = open(cfg->path, mode, cfg->perm)) == -1)
-        return L2_ERROR;
+        return L2_ERR_SYS;
 
     return L2_OK;
 }
@@ -113,13 +113,13 @@
 
     /* parameter checks */
     if ((cfg = (l2_ch_file_t *)ctx->vp) == NULL)
-        return L2_ERROR;
+        return L2_ERR_ARG;
     if (cfg->fd == -1)
-        return L2_ERROR;
+        return L2_ERR_USE;
 
     /* write message to channel file */
     if (write(cfg->fd, buf, buf_size) == -1)
-        return L2_ERROR;
+        return L2_ERR_SYS;
 
     return L2_OK;
 }
@@ -139,9 +139,9 @@
 
     /* parameter checks */
     if ((cfg = (l2_ch_file_t *)ctx->vp) == NULL)
-        return L2_ERROR;
+        return L2_ERR_ARG;
     if (cfg->fd == -1)
-        return L2_ERROR;
+        return L2_ERR_USE;
 
     /* close channel file */
     close(cfg->fd);
@@ -155,7 +155,7 @@
 {
     /* parameter checks */
     if (ctx->vp == NULL) 
-        return L2_ERROR;
+        return L2_ERR_ARG;
 
     /* destroy channel configuration */
     free(ctx->vp);


ossp-pkg/l2/l2_ch_prefix.c 1.8 -> 1.9

--- l2_ch_prefix.c       2001/09/05 13:56:43     1.8
+++ l2_ch_prefix.c       2001/09/06 14:37:53     1.9
@@ -45,7 +45,7 @@
 
     /* allocate private channel configuration */
     if ((cfg = (l2_ch_prefix_t *)malloc(sizeof(l2_ch_prefix_t))) == NULL)
-        return L2_ERROR;
+        return L2_ERR_MEM;
 
     /* initialize configuration with reasonable defaults */
     cfg->timefmt  = NULL;
@@ -73,10 +73,10 @@
     /* argument consistency check */
     if (rv == L2_OK) {
         if (cfg->timezone == NULL)
-            return L2_ERROR;
+            return L2_ERR_ARG;
         if (!(   strcmp(cfg->timezone, "local") == 0 
               || strcmp(cfg->timezone, "utc")   == 0))
-            return L2_ERROR;
+            return L2_ERR_ARG;
     }
 
     return rv;
@@ -105,9 +105,9 @@
         else if (strcmp(cfg->timezone, "utc") == 0)
             tm = gmtime(&t);
         else
-            return L2_ERROR;
+            return L2_ERR_ARG;
         if ((n = strftime(cfg->timebuf, sizeof(cfg->timebuf), cfg->timefmt, tm)) == 0)
-            return L2_ERROR;
+            return L2_ERR_SYS;
         if ((rv = l2_channel_write(downstream, cfg->timebuf, n)) != L2_OK)
             return rv;
     }


ossp-pkg/l2/l2_ch_socket.c 1.13 -> 1.14

--- l2_ch_socket.c       2001/09/05 16:15:38     1.13
+++ l2_ch_socket.c       2001/09/06 14:37:53     1.14
@@ -110,7 +110,7 @@
 
     /* allocate private channel configuration */
     if ((cfg = (l2_ch_socket_t *)malloc(sizeof(l2_ch_socket_t))) == NULL)
-        return L2_ERROR;
+        return L2_ERR_MEM;
 
     /* initialize configuration with reasonable defaults */
     cfg->pszHost   = NULL;
@@ -132,7 +132,7 @@
 
     /* parameter checks */
     if ((cfg = (l2_ch_socket_t *)ctx->vp) == NULL)
-        return L2_ERROR;
+        return L2_ERR_ARG;
 
     /* feed and call generic parameter parsing engine */
     L2_PARAM_SET(pa[0], ipversion, INT,     &cfg->iFamily);
@@ -155,9 +155,9 @@
 
     /* parameter checks */
     if ((cfg = (l2_ch_socket_t *)ctx->vp) == NULL)
-        return L2_ERROR;
+        return L2_ERR_ARG;
     if (cfg->pszHost == NULL)
-        return L2_ERROR;
+        return L2_ERR_USE;
 
     /* open channel socket      */
     if (cfg->iFamily == AF_INET) /* IPv4 */
@@ -181,19 +181,19 @@
             }
 
             else
-                return L2_ERROR; /* what?? we specify IPv4 when it is not? */
+                return L2_ERR_USE; /* what?? we specify IPv4 when it is not? */
         }
 
         else /* host string was not parsable for some reason */
-            return L2_ERROR;
+            return L2_ERR_SYS;
 
         if ((cfg->iSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
-            return L2_ERROR;
+            return L2_ERR_SYS;
 
         if (connect(cfg->iSocket, (struct sockaddr *)&IP4Sockaddr, sizeof(IP4Sockaddr)))
         {
             close(cfg->iSocket);
-            return L2_ERROR;
+            return L2_ERR_SYS;
         }
 
         return L2_OK; /* we are connected */
@@ -220,25 +220,25 @@
             }
 
             else
-                return L2_ERROR; /* what?? we specify IPv6 when it is not? */
+                return L2_ERR_USE; /* what?? we specify IPv6 when it is not? */
         }
 
         else /* host string was not parsable for some reason */
-            return L2_ERROR;
+            return L2_ERR_SYS;
 
         if ((cfg->iSocket = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP)) == -1)
-            return L2_ERROR;
+            return L2_ERR_SYS;
 
         if (connect(cfg->iSocket, (struct sockaddr *)&IP6Sockaddr, sizeof(IP6Sockaddr)))
         {
             close(cfg->iSocket);
-            return L2_ERROR;
+            return L2_ERR_SYS;
         }
 
         return L2_OK; /* we are connected */
     }
 
-    return L2_ERROR;
+    return L2_ERR_USE;
 }
 
 /* write to channel */
@@ -251,9 +251,9 @@
 
     /* parameter checks */
     if ((cfg = (l2_ch_socket_t *)ctx->vp) == NULL)
-        return L2_ERROR;
+        return L2_ERR_ARG;
     if (cfg->iSocket == -1)
-        return L2_ERROR;
+        return L2_ERR_USE;
 
     /* write message to channel socket, but check to make   */
     /* sure that the whole message was successfully written */
@@ -265,7 +265,7 @@
         sizeRemain = sizeRemain - sizeWrite; /* how much is left? */
 
         if (sizeWrite == -1)
-            return L2_ERROR;
+            return L2_ERR_SYS;
     }
 
     return L2_OK;
@@ -286,13 +286,13 @@
 
     /* parameter checks */
     if ((cfg = (l2_ch_socket_t *)ctx->vp) == NULL)
-        return L2_ERROR;
+        return L2_ERR_ARG;
     if (cfg->iSocket == -1)
-        return L2_ERROR;
+        return L2_ERR_USE;
 
     /* close channel socket */
     if ((shutdown(cfg->iSocket, SHUT_RDWR)) || (close(cfg->iSocket)))
-        return L2_ERROR;
+        return L2_ERR_SYS;
 
     return L2_OK;
 }
@@ -302,7 +302,7 @@
 {
     /* parameter checks */
     if (ctx->vp == NULL) 
-        return L2_ERROR;
+        return L2_ERR_ARG;
 
     /* destroy channel configuration */
     free(ctx->vp);


ossp-pkg/l2/l2_ch_syslog.c 1.14 -> 1.15

--- l2_ch_syslog.c       2001/09/05 07:47:12     1.14
+++ l2_ch_syslog.c       2001/09/06 14:37:53     1.15
@@ -47,7 +47,7 @@
 
     /* allocate private channel configuration */
     if ((cfg = (l2_ch_syslog_t *)malloc(sizeof(l2_ch_syslog_t))) == NULL)
-        return L2_ERROR;
+        return L2_ERR_MEM;
 
     /* initialize configuration with reasonable defaults */
     cfg->pszIdent  = NULL;
@@ -71,7 +71,7 @@
 
     /* parameter checks */
     if ((cfg = (l2_ch_syslog_t *)ctx->vp) == NULL)
-        return L2_ERROR;
+        return L2_ERR_ARG;
 
     /* feed and call generic parameter parsing engine */
     L2_PARAM_SET(pa[0], ident,   CHARPTR,  &cfg->pszIdent);
@@ -92,7 +92,7 @@
 
     /* parameter checks */
     if ((cfg = (l2_ch_syslog_t *)ctx->vp) == NULL)
-        return L2_ERROR;
+        return L2_ERR_ARG;
 
     /* open channel syslog */
     openlog(cfg->pszIdent, cfg->iLogopt, cfg->iFacility);
@@ -109,7 +109,7 @@
 
     /* parameter checks */
     if ((cfg = (l2_ch_syslog_t *)ctx->vp) == NULL)
-        return L2_ERROR;
+        return L2_ERR_ARG;
 
     /* write message to channel syslog */
     syslog(cfg->iPriority, buf);
@@ -132,7 +132,7 @@
 
     /* parameter checks */
     if ((cfg = (l2_ch_syslog_t *)ctx->vp) == NULL)
-        return L2_ERROR;
+        return L2_ERR_ARG;
 
     /* close channel syslog */
     closelog();
@@ -145,7 +145,7 @@
 {
     /* parameter checks */
     if (ctx->vp == NULL) 
-        return L2_ERROR;
+        return L2_ERR_ARG;
 
     /* destroy channel configuration */
     free(ctx->vp);


ossp-pkg/l2/l2_channel.c 1.15 -> 1.16

--- l2_channel.c 2001/09/05 19:58:44     1.15
+++ l2_channel.c 2001/09/06 14:37:53     1.16
@@ -33,159 +33,241 @@
 
 #include <string.h>
 
+/* create channel */
 l2_channel_t *l2_channel_create(l2_handler_t *h)
 {
     l2_channel_t *ch;
 
+    /* argument sanity check */
     if (h == NULL)
         return NULL;
+
+    /* allocate channel structure */
     if ((ch = (l2_channel_t *)malloc(sizeof(l2_channel_t))) == NULL)
         return NULL;
+
+    /* initialize channel structure */
     ch->state = L2_CHSTATE_CREATED;
     ch->downstream = NULL;
     memset(&ch->context, 0, sizeof(l2_context_t));
     memcpy(&ch->handler, h, sizeof(l2_handler_t));
-    if (ch->handler.create(&ch->context, ch) == L2_ERROR) {
-        free(ch);
-        return NULL;
+
+    /* (optionally) perform create operation in handler */
+    if (ch->handler.create != NULL) {
+        if (ch->handler.create(&ch->context, ch) != L2_OK) {
+            free(ch);
+            return NULL;
+        }
     }
+
     return ch;
 }
 
+/* configure channel */
 l2_result_t l2_channel_configure(l2_channel_t *ch, const char *fmt, ...)
 {
     l2_result_t rv;
     va_list ap;
 
+    /* argument sanity check */
     if (ch == NULL || fmt == NULL)
-        return L2_ERROR;
+        return L2_ERR_ARG;
+
+    /* make sure the channel is in state "created" */
     if (ch->state != L2_CHSTATE_CREATED)
-        return L2_ERROR;
+        return L2_ERR_USE;
+
+    /* pass operation to handler */
+    rv = L2_OK;
     va_start(ap, fmt);
-    rv = ch->handler.configure(&ch->context, ch, fmt, ap);
+    if (ch->handler.configure != NULL)
+        rv = ch->handler.configure(&ch->context, ch, fmt, ap);
     va_end(ap);
+
     return rv;
 }
 
+/* open channel (stack) */
 l2_result_t l2_channel_open(l2_channel_t *ch)
 {
     l2_result_t rv;
 
+    /* argument sanity check */
     if (ch == NULL)
-        return L2_ERROR;
+        return L2_ERR_ARG;
+
+    /* make sure channel is in state "created" */
     if (ch->state != L2_CHSTATE_CREATED)
-        return L2_ERROR;
+        return L2_ERR_USE;
+
+    /* skip empty open handlers on channel stack */
     while (ch != NULL && ch->handler.open == NULL) {
         ch->state = L2_CHSTATE_OPENED;
         ch = ch->downstream;
     }
     if (ch == NULL)
-        return L2_ERROR;
+        return L2_ERR_USE;
+
+    /* pass operation to handler */
     rv = ch->handler.open(&ch->context, ch);
     if (rv == L2_OK)
         ch->state = L2_CHSTATE_OPENED;
+
     return rv;
 }
 
+/* write to channel (stack) */
 l2_result_t l2_channel_write(l2_channel_t *ch, const char *buf, size_t bufsize)
 {
     l2_result_t rv;
 
-    if (ch == NULL)
-        return L2_ERROR;
+    /* argument sanity check */
+    if (ch == NULL || buf == NULL)
+        return L2_ERR_ARG;
+
+    /* make sure channel is in state "opened" */
     if (ch->state != L2_CHSTATE_OPENED)
-        return L2_ERROR;
-    if (buf == NULL)
-        return L2_ERROR;
+        return L2_ERR_USE;
+
+    /* short circuiting */
     if (bufsize == 0)
         return L2_OK;
+
+    /* walk to next available write handler */
     while (ch != NULL && ch->handler.write == NULL)
         ch = ch->downstream;
     if (ch == NULL)
-        return L2_ERROR;
+        return L2_ERR_USE;
+
+    /* pass operation to handler */
     rv = ch->handler.write(&ch->context, ch, buf, bufsize);
+
     return rv;
 }
 
+/* flush channel (stack) */
 l2_result_t l2_channel_flush(l2_channel_t *ch)
 {
     l2_result_t rv;
 
+    /* argument sanity check */
     if (ch == NULL)
-        return L2_ERROR;
+        return L2_ERR_ARG;
+
+    /* make sure channel is in state "opened" */
     if (ch->state != L2_CHSTATE_OPENED)
-        return L2_ERROR;
+        return L2_ERR_USE;
+
+    /* walk to next available flush handler */
     while (ch != NULL && ch->handler.flush == NULL)
         ch = ch->downstream;
     if (ch == NULL)
-        return L2_ERROR;
+        return L2_ERR_USE;
+
+    /* pass operation to handler */
     rv = ch->handler.flush(&ch->context, ch);
+
     return rv;
 }
 
+/* close channel (stack) */
 l2_result_t l2_channel_close(l2_channel_t *ch)
 {
     l2_result_t rv;
 
+    /* argument sanity check */
     if (ch == NULL)
-        return L2_ERROR;
+        return L2_ERR_ARG;
+    
+    /* make sure channel is in state "opened" */
     if (ch->state != L2_CHSTATE_OPENED)
-        return L2_ERROR;
+        return L2_ERR_USE;
+
+    /* walk to next available close handler */
     while (ch != NULL && ch->handler.close == NULL) {
         ch->state = L2_CHSTATE_CREATED;
         ch = ch->downstream;
     }
     if (ch == NULL)
-        return L2_ERROR;
+        return L2_ERR_USE;
+
+    /* pass operation to handler */
     rv = ch->handler.close(&ch->context, ch);
     if (rv == L2_OK)
         ch->state = L2_CHSTATE_CREATED;
+
     return rv;
 }
 
+/* destroy channel */
 l2_result_t l2_channel_destroy(l2_channel_t *ch)
 {
     l2_result_t rv;
 
+    /* argument sanity check */
     if (ch == NULL)
-        return L2_ERROR;
+        return L2_ERR_ARG;
+
+    /* make sure channel is in state "opened" */
     if (ch->state == L2_CHSTATE_OPENED)
         if ((rv = l2_channel_close(ch)) != L2_OK)
             return rv;
+
+    /* walk to next available destroy handler */
     while (ch != NULL && ch->handler.destroy == NULL)
         ch = ch->downstream;
+    
+    /* pass operation to handler */
     if (ch != NULL)
         rv = ch->handler.destroy(&ch->context, ch);
     else
         rv = L2_OK;
+
+    /* free channel structure */
     free(ch);
+
     return rv;
 }
 
+/* stack channel on top of another channel */
 l2_result_t l2_channel_stack(l2_channel_t *ch, l2_channel_t *chTop)
 {
+    /* argument sanity check */
     if (ch == NULL || chTop == NULL)
-        return L2_ERROR;
+        return L2_ERR_ARG;
+
+    /* make sure both channels are in state "created" */
     if (   ch->state    != L2_CHSTATE_CREATED
         || chTop->state != L2_CHSTATE_CREATED)
-        return L2_ERROR;
+        return L2_ERR_USE;
+
+    /* make sure top channel is a filter channel */
     if (chTop->handler.type != L2_CHANNEL_FILTER)
-        return L2_ERROR;
+        return L2_ERR_USE;
+
+    /* stack the channels */
     chTop->downstream = ch;
+
     return L2_OK;
 }
 
+/* return downstream channel */
 l2_channel_t *l2_channel_downstream(l2_channel_t *ch)
 {
+    /* argument sanity check */
     if (ch == NULL)
         return NULL;
+
     return ch->downstream;
 }
 
+/* return channel type */
 l2_chtype_t l2_channel_type(l2_channel_t *ch)
 {
+    /* argument sanity check */
     if (ch == NULL)
         return NULL;
+
     return ch->handler.type;
 }
 


ossp-pkg/l2/l2_stream.c 1.13 -> 1.14

--- l2_stream.c  2001/09/05 14:10:00     1.13
+++ l2_stream.c  2001/09/06 14:37:53     1.14
@@ -31,112 +31,142 @@
 #include "l2.h"
 #include "l2_p.h"
 
+/* create stream */
 l2_stream_t *l2_stream_create(void)
 {
     l2_stream_t *st;
     int i;
     
+    /* allocate stream structure */ 
     if ((st = (l2_stream_t *)malloc(sizeof(l2_stream_t))) == NULL)
         return NULL;
+
+    /* initialize stream structure */
     st->levelmask = 0;
     for (i = 0; i < L2_MAX_CHANNELS; i++)
         st->channels[i].ch = NULL;
     for (i = 0; i < L2_MAX_FORMATTERS; i++)
         st->formatters[i].cb = NULL;
+
     return st;
 }
 
+/* attach channel to stream */
 l2_result_t l2_stream_channel(l2_stream_t *st, l2_channel_t *ch, unsigned int levelmask)
 {
-    int i;
     l2_channel_t *chC;
     l2_channel_t *chN;
+    int i;
 
+    /* argument sanity check */
     if (st == NULL || ch == NULL || levelmask == 0)
-        return L2_ERROR;
+        return L2_ERR_ARG;
 
     /* make sure the stack of channels consists of zero or more
        filter channels followed by exactly one output channel */
     for (chC = ch; (chN = l2_channel_downstream(chC)) != NULL; chC = chN)
         if (l2_channel_type(chC) != L2_CHANNEL_FILTER)
-            return L2_ERROR;
+            return L2_ERR_USE;
     if (l2_channel_type(chC) != L2_CHANNEL_OUTPUT)
-        return L2_ERROR;
+        return L2_ERR_USE;
 
     /* find next free channel position in channel array */
     for (i = 0; i < L2_MAX_CHANNELS && st->channels[i].ch != NULL; i++)
         ;
     if (i == L2_MAX_CHANNELS)
-        return L2_ERROR;
+        return L2_ERR_MEM;
 
-    /* attach this channel */
+    /* attach channel to stream */
     st->channels[i].ch = ch;
     st->channels[i].levelmask = levelmask;
 
     return L2_OK;
 }
 
+/* attach formatter to stream */
 l2_result_t l2_stream_formatter(l2_stream_t *st, char id, l2_formatter_t cb, l2_context_t *ctx)
 {
     int i;
 
+    /* argument sanity check */
     if (st == NULL || id == '\0' || cb == NULL)
-        return L2_ERROR;
+        return L2_ERR_ARG;
+
+    /* find next free formatter position in formatter array */
     for (i = 0; i < L2_MAX_FORMATTERS && st->formatters[i].cb != NULL; i++)
         ;
     if (i == L2_MAX_FORMATTERS)
-        return L2_ERROR;
+        return L2_ERR_MEM;
+
+    /* attach formatter to stream */
     st->formatters[i].id   = id;
     st->formatters[i].ctx  = ctx;
     st->formatters[i].cb   = cb;
+
     return L2_OK;
 }
 
+/* set stream level mask */
 l2_result_t l2_stream_levels(l2_stream_t *st, unsigned int levelmask)
 {
+    /* argument sanity check */
     if (st == NULL)
-        return L2_ERROR;
+        return L2_ERR_ARG;
+
+    /* override global level mask */
     st->levelmask = levelmask;
+
     return L2_OK;
 }
 
+/* log a message to stream */
 l2_result_t l2_stream_log(l2_stream_t *st, unsigned int level, const char *fmt, ...)
 {
     va_list ap;
     l2_result_t rv;
 
+    /* argument sanity check */
     if (st == NULL || level == 0 || fmt == NULL)
-        return L2_ERROR;
+        return L2_ERR_ARG;
+
+    /* pass-through to va_list-based variant */
     va_start(ap, fmt);
     rv = l2_stream_vlog(st, level, fmt, ap);
     va_end(ap);
+
     return rv;
 }
 
+/* indirect callback function from l2_stream_vlog for flushing */
 static int l2_stream_vlog_flush(l2_util_format_t *vfmt)
 {
+    /* we do no format buffer flushing */
     return -1;
 }
 
+/* indirect callback function from l2_stream_vlog for formatting */
 static void l2_stream_vlog_format(
     l2_util_format_t *vfmt,
     char *cPrefix, char *cPad, char **cppOut, size_t *npOutLen,
     char *cpBuf, int nBufLenMax, char *cpParam, char cId, va_list *apArgs)
 {
     l2_stream_t *st = (l2_stream_t *)(vfmt->data[0].vp);
+    l2_result_t rv;
     int i;
 
-    /* init result */
+    /* init formatting result */
     *cPrefix = '\0';
     *cPad = ' ';
     *cppOut = NULL;
     *npOutLen = 0;
 
-    /* iterate over all configured formatters */
+    /* iterate over all configured L2 formatters */
     for (i = 0; i < L2_MAX_FORMATTERS && st->formatters[i].cb != NULL; i++) {
         if (st->formatters[i].id == cId) {
-            if (st->formatters[i].cb(st->formatters[i].ctx, cId, cpParam, 
-                                     cpBuf, nBufLenMax, npOutLen, apArgs) == L2_OK) {
+            rv = st->formatters[i].cb(st->formatters[i].ctx, cId, cpParam, 
+                                      cpBuf, nBufLenMax, npOutLen, apArgs);
+            vfmt->data[1].i = (int)rv;
+            if (rv == L2_OK) {
                 *cppOut = cpBuf;
                 break;
             }
@@ -145,6 +175,7 @@
     return;
 }
 
+/* log a message to stream (va_list-variant) */
 l2_result_t l2_stream_vlog(l2_stream_t *st, unsigned int level, const char *fmt, va_list ap)
 {
     int i;
@@ -153,17 +184,18 @@
     l2_result_t rv;
     l2_util_format_t vfmt;
 
+    /* argument sanity check */
     if (st == NULL || fmt == NULL || ap == NULL)
-        return L2_ERROR;
+        return L2_ERR_ARG;
 
     /* make sure only a single level is specified */
     for (l = level, j = 0; l != 0; l = (l >> 1))
         if (l & 0x1)
             j++;
     if (j != 1)
-        return L2_ERROR;
+        return L2_ERR_ARG;
 
-    /* check whether level is globally enabled */
+    /* check whether global level mask already stops processing */
     if (!(st->levelmask & level))
         return L2_OK;
 
@@ -171,21 +203,23 @@
     vfmt.curpos = st->message;
     vfmt.endpos = st->message + L2_MAX_MSGSIZE;
     vfmt.data[0].vp = st;
+    vfmt.data[1].i  = L2_ERR_FMT;
     vfmt.flush  = l2_stream_vlog_flush;
     vfmt.format = l2_stream_vlog_format;
     if ((len = l2_util_format(&vfmt, fmt, ap)) == -1)
-        return L2_ERROR;
+        return (l2_result_t)(vfmt.data[1].i);
     if (len == 0)
-        return L2_ERROR;
+        return L2_ERR_FMT;
 
     /* make sure a trailing newline exists */
     if (st->message[len-1] != '\n') {
         if (len == L2_MAX_MSGSIZE)
-            return L2_ERROR;
+            return L2_ERR_MEM;
         st->message[len++] = '\n';
         st->message[len]   = '\0';
     }
 
+    /* write message to zero or more channels */
     rv = L2_OK;
     for (i = 0; i < L2_MAX_CHANNELS && st->channels[i].ch != NULL; i++) {
         if (st->channels[i].levelmask & level) {
@@ -196,15 +230,24 @@
     return rv;
 }
 
+/* destroy stream */
 l2_result_t l2_stream_destroy(l2_stream_t *st)
 {
     int i;
+    l2_result_t rv;
 
+    /* argument sanity check */
     if (st == NULL)
-        return L2_ERROR;
+        return L2_ERR_ARG;
+
+    /* destroy all attached channels */
     for (i = 0; i < L2_MAX_CHANNELS && st->channels[i].ch != NULL; i++)
-        l2_channel_destroy(st->channels[i].ch);
+        if ((rv = l2_channel_destroy(st->channels[i].ch)) != L2_OK)
+            return rv;
+
+    /* free stream structure */
     free(st);
+
     return L2_OK;
 }
 


ossp-pkg/l2/l2_ut_fmtcb.c 1.1 -> 1.2

--- l2_ut_fmtcb.c        2001/09/06 11:56:15     1.1
+++ l2_ut_fmtcb.c        2001/09/06 14:37:53     1.2
@@ -41,8 +41,10 @@
 
     cpStr = va_arg(*ap, char *);
     nStr  = (size_t)va_arg(*ap, int);
+    if (cpStr == NULL)
+        return L2_ERR_ARG;
     if (nStr > bufsize)
-        return L2_ERROR;
+        return L2_ERR_MEM;
     memcpy(bufptr, cpStr, nStr);
     *buflen = nStr;
     return L2_OK;
@@ -71,6 +73,10 @@
     ucpData = va_arg(*ap, unsigned char *);
     nData   = (size_t)va_arg(*ap, int);
 
+    /* argument sanity check */
+    if (ucpData == NULL)
+        return L2_ERR_ARG;
+
     /* 
      * textual dumping: "foo\1bar" -> "foo\x01bar"
      */
@@ -81,18 +87,18 @@
             uc = *cpI++;
             if (isprint((int)uc)) {
                 if ((char *)cpO >= (bufptr+bufsize))
-                    return L2_ERROR;
+                    return L2_ERR_MEM;
                 *cpO++ = uc;
             }
             else if (uc == '\\') {
                 if ((char *)(cpO+1) >= (bufptr+bufsize))
-                    return L2_ERROR;
+                    return L2_ERR_MEM;
                 *cpO++ = '\\';
                 *cpO++ = '\\';
             }
             else {
                 if ((char *)(cpO+3) >= (bufptr+bufsize))
-                    return L2_ERROR;
+                    return L2_ERR_MEM;
                 *cpO++ = '\\';
                 *cpO++ = 'x';
                 *cpO++ = hex_table[(uc >> 4) & 0xf];
@@ -107,7 +113,7 @@
      */
     else if (strcmp(param, "hex") == 0) {
         if (((nData * 3) - 1) > bufsize)
-            return L2_ERROR;
+            return L2_ERR_MEM;
         cpO = (unsigned char *)bufptr;
         for (i = 0; i < nData; i++) {
             uc = *ucpData++;
@@ -135,7 +141,7 @@
             base64_out[2] = ((base64_in[1] & 0x0f) << 2) + (base64_in[2] >> 6);
             base64_out[3] = ( base64_in[2] & 0x3f);
             if (n + 4 > bufsize)
-                return L2_ERROR;
+                return L2_ERR_MEM;
             bufptr[n++] = base64_table[base64_out[0]];
             bufptr[n++] = base64_table[base64_out[1]];
             bufptr[n++] = base64_table[base64_out[2]];
@@ -150,7 +156,7 @@
             base64_out[1] = ((base64_in[0] & 0x03) << 4) + (base64_in[1] >> 4);
             base64_out[2] = ((base64_in[1] & 0x0f) << 2) + (base64_in[2] >> 6);
             if (n + 4 > bufsize)
-                return L2_ERROR;
+                return L2_ERR_MEM;
             bufptr[n++] = base64_table[base64_out[0]];
             bufptr[n++] = base64_table[base64_out[1]];
             if (nData == 1)
@@ -160,13 +166,13 @@
             bufptr[n++] = '=';
         }
         if (n >= bufsize)
-            return L2_ERROR;
+            return L2_ERR_MEM;
         *buflen = n;
     }
 
     /* anything else is an unknown dumping method... */
     else
-        return L2_ERROR;
+        return L2_ERR_ARG;
 
     return L2_OK;
 }


ossp-pkg/l2/l2_ut_format.c 1.1 -> 1.2

--- l2_ut_format.c       2001/09/04 13:52:59     1.1
+++ l2_ut_format.c       2001/09/06 14:37:53     1.2
@@ -1023,6 +1023,8 @@
                         vbuff->format(vbuff,
                                       &prefix_char, &pad_char, &s, &s_len, 
                                       num_buf, NUM_BUF_SIZE, extinfo, *fmt, &ap);
+                        if (s == NULL)
+                            return -1;
                     }
                     if (s == NULL) {
                         char_buf[0] = '%';


ossp-pkg/l2/l2_ut_level.c 1.1 -> 1.2

--- l2_ut_level.c        2001/09/05 13:32:04     1.1
+++ l2_ut_level.c        2001/09/06 14:37:53     1.2
@@ -62,7 +62,7 @@
             levelmask &= ~(l2s_table[i].level);
             l = strlen(l2s_table[i].string) + 1;
             if (len < l)
-                return L2_ERROR;
+                return L2_ERR_MEM;
             sprintf(string+(maxlen-len), "%s%c", l2s_table[i].string, sep);
             len -= l;
         }
@@ -71,7 +71,7 @@
         sprintf(hexbuf, "0x%x", levelmask);
         l = strlen(hexbuf) + 1;
         if (len < l)
-            return L2_ERROR;
+            return L2_ERR_MEM;
         sprintf(string+(maxlen-len), "%s%c", hexbuf, sep);
         len -= l;
     }
@@ -131,7 +131,7 @@
                 *levelmask |= hexval(cpB+2, cpE);
             }
             else
-                return L2_ERROR;
+                return L2_ERR_ARG;
         }
     }
     return L2_OK;


ossp-pkg/l2/l2_ut_param.c 1.3 -> 1.4

--- l2_ut_param.c        2001/09/05 13:34:58     1.3
+++ l2_ut_param.c        2001/09/06 14:37:53     1.4
@@ -40,7 +40,7 @@
     int i; 
 
     if (pa == NULL || fmt == NULL || ap == NULL)
-        return L2_ERROR;
+        return L2_ERR_ARG;
     cpE = fmt;
     while (*cpE != '\0') {
         /* determine begin of parameter name */
@@ -102,7 +102,7 @@
             }
         }
         if (!ok)
-            return L2_ERROR;
+            return L2_ERR_USE;
     }
     return L2_OK;
 }

CVSTrac 2.0.1