Implemented handling of multi-sentences replies.
[routeros-api.git] / src / main.c
index 3273091..4ad7e8f 100644 (file)
@@ -165,6 +165,30 @@ static int reply_add_keyval (mt_reply_t *r, const char *key, /* {{{ */
        return (0);
 } /* }}} int reply_add_keyval */
 
+static void reply_dump (const mt_reply_t *r) /* {{{ */
+{
+       if (r == NULL)
+               return;
+
+       printf ("=== BEGIN REPLY ===\n"
+                       "Address: %p\n"
+                       "Status: %s\n",
+                       (void *) r, r->status);
+       if (r->params_num > 0)
+       {
+               unsigned int i;
+
+               printf ("Arguments:\n");
+               for (i = 0; i < r->params_num; i++)
+                       printf (" %3u: %s = %s\n", i, r->keys[i], r->values[i]);
+       }
+       if (r->next != NULL)
+               printf ("Next: %p\n", (void *) r->next);
+       printf ("=== END REPLY ===\n");
+
+       reply_dump (r->next);
+} /* }}} void reply_dump */
+
 static void reply_free (mt_reply_t *r) /* {{{ */
 {
        mt_reply_t *next;
@@ -324,6 +348,7 @@ static int send_command (mt_connection_t *c, /* {{{ */
        if (status != 0)
                return (status);
 
+       mt_debug ("send_command: command = %s;\n", command);
        status = buffer_add (&buffer_ptr, &buffer_size, command);
        if (status != 0)
                return (status);
@@ -333,6 +358,7 @@ static int send_command (mt_connection_t *c, /* {{{ */
                if (args[i] == NULL)
                        return (EINVAL);
 
+               mt_debug ("send_command: arg[%zu] = %s;\n", i, args[i]);
                status = buffer_add (&buffer_ptr, &buffer_size, args[i]);
                if (status != 0)
                        return (status);
@@ -452,17 +478,17 @@ static int read_word (mt_connection_t *c, /* {{{ */
        return (0);
 } /* }}} int buffer_decode_next */
 
-static mt_reply_t *receive_reply (mt_connection_t *c) /* {{{ */
+static mt_reply_t *receive_sentence (mt_connection_t *c) /* {{{ */
 {
        char buffer[4096];
        size_t buffer_size;
        int status;
 
-       mt_reply_t *head;
-       mt_reply_t *tail;
+       mt_reply_t *r;
 
-       head = NULL;
-       tail = NULL;
+       r = reply_alloc ();
+       if (r == NULL)
+               return (NULL);
 
        while (42)
        {
@@ -479,33 +505,11 @@ static mt_reply_t *receive_reply (mt_connection_t *c) /* {{{ */
 
                if (buffer[0] == '!') /* {{{ */
                {
-                       mt_reply_t *tmp;
-
-                       tmp = reply_alloc ();
-                       if (tmp == NULL)
-                       {
-                               status = ENOMEM;
-                               break;
-                       }
-
-                       tmp->status = strdup (&buffer[1]);
-                       if (tmp->status == NULL)
-                       {
-                               reply_free (tmp);
-                               status = ENOMEM;
+                       if (r->status != NULL)
+                               free (r->status);
+                       r->status = strdup (&buffer[1]);
+                       if (r->status == NULL)
                                break;
-                       }
-
-                       if (tail == NULL)
-                       {
-                               head = tmp;
-                               tail = tmp;
-                       }
-                       else
-                       {
-                               tail->next = tmp;
-                               tail = tmp;
-                       }
                } /* }}} if (buffer[0] == '!') */
                else if (buffer[0] == '=') /* {{{ */
                {
@@ -522,20 +526,54 @@ static mt_reply_t *receive_reply (mt_connection_t *c) /* {{{ */
                        *val = 0;
                        val++;
 
-                       reply_add_keyval (tail, key, val);
+                       reply_add_keyval (r, key, val);
                } /* }}} if (buffer[0] == '=') */
                else
                {
-                       printf ("Ignoring unknown word: %s\n", buffer);
+                       mt_debug ("receive_sentence: Ignoring unknown word: %s\n", buffer);
                }
        } /* while (42) */
        
-       if (status != 0)
+       if (r->status == NULL)
        {
-               reply_free (head);
+               reply_free (r);
                return (NULL);
        }
 
+       return (r);
+} /* }}} mt_reply_t *receive_sentence */
+
+static mt_reply_t *receive_reply (mt_connection_t *c) /* {{{ */
+{
+       mt_reply_t *head;
+       mt_reply_t *tail;
+
+       head = NULL;
+       tail = NULL;
+
+       while (42)
+       {
+               mt_reply_t *tmp;
+
+               tmp = receive_sentence (c);
+               if (tmp == NULL)
+                       break;
+
+               if (tail == NULL)
+               {
+                       head = tmp;
+                       tail = tmp;
+               }
+               else
+               {
+                       tail->next = tmp;
+                       tail = tmp;
+               }
+
+               if (strcmp ("done", tmp->status) == 0)
+                       break;
+       } /* while (42) */
+       
        return (head);
 } /* }}} mt_reply_t *receive_reply */
 
@@ -598,6 +636,7 @@ static int login2_handler (mt_connection_t *c, const mt_reply_t *r, /* {{{ */
                return (EINVAL);
 
        printf ("login2_handler has been called.\n");
+       reply_dump (r);
 
        return (0);
 } /* }}} int login2_handler */
@@ -671,6 +710,7 @@ static int login_handler (mt_connection_t *c, const mt_reply_t *r, /* {{{ */
                return (EINVAL);
 
        printf ("login_handler has been called.\n");
+       reply_dump (r);
 
        login_data = user_data;
        if (login_data == NULL)