Doc fixes.
[bertos.git] / mware / readline.c
old mode 100755 (executable)
new mode 100644 (file)
index 873b14a..4c3b3a8
@@ -1,6 +1,31 @@
 /**
  * \file
  * <!--
+ * This file is part of BeRTOS.
+ *
+ * Bertos is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * As a special exception, you may use this file as part of a free software
+ * library without restriction.  Specifically, if other files instantiate
+ * templates or use macros or inline functions from this file, or you compile
+ * this file and link it with other files to produce an executable, this
+ * file does not by itself cause the resulting executable to be covered by
+ * the GNU General Public License.  This exception does not however
+ * invalidate any other reasons why the executable file might be covered by
+ * the GNU General Public License.
+ *
  * Copyright 2004 Develer S.r.l. (http://www.develer.com/)
  * Copyright 2004 Giovanni Bajo
  * All Rights Reserved.
  * \author Giovanni Bajo <rasky@develer.com>
  */
 
-/*#*
- *#* $Log$
- *#* Revision 1.2  2006/07/19 12:56:28  bernie
- *#* Convert to new Doxygen style.
- *#*
- *#* Revision 1.1  2006/06/01 12:27:39  marco
- *#* Added utilities for protocols
- *#*
- *#*/
 
 #include "readline.h"
 
@@ -59,6 +75,7 @@
 #include <cfg/debug.h>
 
 #include <stdio.h>
+#include <drv/ser.h>
 
 
 /// Enable compilation of the unit test code
@@ -149,8 +166,14 @@ INLINE void rl_putc(const struct RLContext* ctx, char ch)
 static bool rl_getc(const struct RLContext* ctx, int* ch)
 {
        int c = ctx->get(ctx->get_param);
+
        if (c == EOF)
+       {
+               if (ctx->clear)
+                       ctx->clear(ctx->clear_param);
+
                return false;
+       }
 
        if (c == 0x1B)
        {
@@ -357,7 +380,7 @@ const char* rl_readline(struct RLContext* ctx)
                char ch;
                int c;
 
-               ASSERT(ctx->history - ctx->real_history + i < HISTORY_SIZE);
+               ASSERT(ctx->history - ctx->real_history + ctx->line_pos < HISTORY_SIZE);
 
                if (!rl_getc(ctx, &c))
                        return NULL;
@@ -372,12 +395,17 @@ const char* rl_readline(struct RLContext* ctx)
                        if (!ctx->match)
                                return false;
 
-                       complete_word(ctx, &i);
+                       complete_word(ctx, &ctx->line_pos);
                        continue;
                }
 
                if (c == '\r' || c == '\n')
                {
+                       if (ctx->prompt)
+                               rl_puts(ctx, ctx->prompt);
+
+                       // Terminate line
+                       insert_chars(ctx, &ctx->line_pos, NULL, 0);
                        rl_puts(ctx, "\r\n");
                        break;
                }
@@ -386,9 +414,9 @@ const char* rl_readline(struct RLContext* ctx)
                //  the start of the line
                if (c == '\b')
                {
-                       if (ctx->history[i] != '\0')
+                       if (ctx->history[ctx->line_pos] != '\0')
                        {
-                               --i;
+                               --ctx->line_pos;
                                rl_puts(ctx, "\b \b");
                        }
                        continue;
@@ -397,18 +425,22 @@ const char* rl_readline(struct RLContext* ctx)
                // Add a character to the buffer, if possible
                ch = (char)c;
                ASSERT2(ch == c, "a special key was not properly handled");
-               if (insert_chars(ctx, &i, &ch, 1))
+               if (insert_chars(ctx, &ctx->line_pos, &ch, 1))
+               {
                        rl_putc(ctx, ch);
+               }
                else
+               {
                        beep(ctx);
+               }
        }
 
-       ctx->history_pos = i+1;
-       while (ctx->history[i] != '\0')
-               --i;
+       ctx->history_pos = ctx->line_pos + 1;
+       while (ctx->history[ctx->line_pos] != '\0')
+               --ctx->line_pos;
 
        // Do not store empty lines in the history
-       if (i == ctx->history_pos - 1)
+       if (ctx->line_pos == ctx->history_pos - 1)
                ctx->history_pos -= 1;
 
 #if DEBUG_DUMP_HISTORY
@@ -417,7 +449,7 @@ const char* rl_readline(struct RLContext* ctx)
 
        // Since the current pointer now points to the separator, we need
        //  to return the first character
-       return &ctx->history[i+1];
+       return &ctx->history[ctx->line_pos + 1];
 }