MicroShellというライブラリのバッファ編集機能に惹かれて強引にSerialクラスっぽくラップしたもの

https://www.cubeatsystems.com/microshell/index.html 様に感謝 やっつけすぎて申し訳ないが技量が足りない...

Files at this revision

API Documentation at this revision

Comitter:
beingblock
Date:
Thu Oct 19 07:39:57 2017 +0000
Commit message:
??

Changed in this revision

MicroShell.cpp Show annotated file Show diff for this revision Revisions of this file
MicroShell.h Show annotated file Show diff for this revision Revisions of this file
core/microshell.c Show annotated file Show diff for this revision Revisions of this file
core/microshell.h Show annotated file Show diff for this revision Revisions of this file
core/msconf.h Show annotated file Show diff for this revision Revisions of this file
core/mscore.c Show annotated file Show diff for this revision Revisions of this file
core/mscore.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r ff53d8f5146a MicroShell.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MicroShell.cpp	Thu Oct 19 07:39:57 2017 +0000
@@ -0,0 +1,81 @@
+#include "MicroShell.h"
+#include "core/microshell.h"
+
+Serial *msh_con = NULL;
+MICROSHELL msh_struct;
+MSCORE_ACTION msh_action;
+
+void msh_putc(char c)
+{
+    msh_con -> putc(c);
+}
+char msh_getc(void)
+{
+    return msh_con -> getc();
+}
+void msh_act_hook(MSCORE_ACTION action)
+{
+    msh_action = action;
+}
+
+MicroShell::MicroShell(PinName tx, PinName rx, int baud)
+{
+    if(msh_con==NULL) {
+        msh_con = new Serial(tx,rx,baud);
+        microshell_init(&msh_struct, msh_putc, msh_getc, msh_act_hook);
+    }
+}
+void MicroShell::putc(int c)
+{
+    msh_putc(c);
+}
+void MicroShell::puts(const char *s)
+{
+    while (*s) {
+        msh_putc(*s++);
+    }
+}
+int MicroShell::getc()
+{
+    return msh_getc();
+}
+char *MicroShell::gets(char *s, int size)
+{
+    return microshell_getline(&msh_struct, s, size);
+}
+int MicroShell::printf(const char* format, ...)
+{
+    int ret;
+    va_list args;
+    va_start(args, format);
+    ret = msh_con -> vprintf(format, args);
+    va_end(args);
+    return ret;
+}
+int MicroShell::scanf(const char* format, ...)
+{
+    char buf[256];
+    microshell_getline(&msh_struct, buf, sizeof(buf));
+
+    int ret;
+    va_list args;
+    va_start(args, format);
+    ret = vsscanf(buf, format, args);
+    va_end(args);
+    return ret;
+}
+int MicroShell::vprintf(const char* format, std::va_list args)
+{
+    int ret;
+    ret = msh_con -> vprintf(format, args);
+    return ret;
+}
+int MicroShell::vscanf(const char* format, std::va_list args)
+{
+    char buf[256];
+    microshell_getline(&msh_struct, buf, sizeof(buf));
+
+    int ret;
+    ret = vsscanf(buf, format, args);
+    return ret;
+}
diff -r 000000000000 -r ff53d8f5146a MicroShell.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MicroShell.h	Thu Oct 19 07:39:57 2017 +0000
@@ -0,0 +1,21 @@
+#ifndef MICROSHELL_CLASS_H
+#define MICROSHELL_CLASS_H
+
+#include "mbed.h"
+
+class MicroShell
+{
+public:
+    MicroShell(PinName tx, PinName rx, int baud = 9600);
+
+    void putc(int c);
+    void puts(const char *s);
+    int getc();
+    char *gets(char *s, int size);
+    int printf(const char* format, ...);
+    int scanf( const char* format, ...);
+    int vprintf(const char* format, std::va_list args);
+    int vscanf( const char* format, std::va_list args);
+};
+
+#endif
\ No newline at end of file
diff -r 000000000000 -r ff53d8f5146a core/microshell.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/core/microshell.c	Thu Oct 19 07:39:57 2017 +0000
@@ -0,0 +1,321 @@
+/**
+ * @file      microshell.c
+ * @author    Shinichiro Nakamura (CuBeatSystems)
+ * ===============================================================
+ * MicroShell (Version 0.0.2)
+ * Copyright (c) 2016, 2017 Shinichiro Nakamura (CuBeatSystems)
+ * ===============================================================
+ * The MIT License : https://opensource.org/licenses/MIT
+ *
+ * Copyright (c) 2016, 2017 Shinichiro Nakamura (CuBeatSystems)
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include "microshell.h"
+
+#define PRINT_ACTION_ENABLED    (0)
+#define PRINT_CODE_ENABLED      (0)
+
+#if PRINT_ACTION_ENABLED
+
+static void uart_puts(MICROSHELL *handle, char *str)
+{
+    while (*str) {
+        handle->uart_putc(*str++);
+    }
+}
+
+static void print_action(MICROSHELL *handle, MSCORE_ACTION action)
+{
+    switch (action) {
+        case MSCORE_ACTION_IGNORE:      uart_puts(handle, "[IGNORE]");      break;
+        case MSCORE_ACTION_DISPLAYABLE: uart_puts(handle, "[DISPLAYABLE]"); break;
+        case MSCORE_ACTION_INSERT:      uart_puts(handle, "[INSERT]");      break;
+        case MSCORE_ACTION_ENTER:       uart_puts(handle, "[ENTER]");       break;
+        case MSCORE_ACTION_TAB:         uart_puts(handle, "[TAB]");         break;
+        case MSCORE_ACTION_BS:          uart_puts(handle, "[BS]");          break;
+        case MSCORE_ACTION_DEL:         uart_puts(handle, "[DEL]");         break;
+        case MSCORE_ACTION_CTRL_A:      uart_puts(handle, "[Ctrl+A]");      break;
+        case MSCORE_ACTION_CTRL_B:      uart_puts(handle, "[Ctrl+B]");      break;
+        case MSCORE_ACTION_CTRL_C:      uart_puts(handle, "[Ctrl+C]");      break;
+        case MSCORE_ACTION_CTRL_D:      uart_puts(handle, "[Ctrl+D]");      break;
+        case MSCORE_ACTION_CTRL_E:      uart_puts(handle, "[Ctrl+E]");      break;
+        case MSCORE_ACTION_CTRL_F:      uart_puts(handle, "[Ctrl+F]");      break;
+        case MSCORE_ACTION_CTRL_G:      uart_puts(handle, "[Ctrl+G]");      break;
+        case MSCORE_ACTION_CTRL_H:      uart_puts(handle, "[Ctrl+H]");      break;
+        case MSCORE_ACTION_CTRL_I:      uart_puts(handle, "[Ctrl+I]");      break;
+        case MSCORE_ACTION_CTRL_J:      uart_puts(handle, "[Ctrl+J]");      break;
+        case MSCORE_ACTION_CTRL_K:      uart_puts(handle, "[Ctrl+K]");      break;
+        case MSCORE_ACTION_CTRL_L:      uart_puts(handle, "[Ctrl+L]");      break;
+        case MSCORE_ACTION_CTRL_M:      uart_puts(handle, "[Ctrl+M]");      break;
+        case MSCORE_ACTION_CTRL_N:      uart_puts(handle, "[Ctrl+N]");      break;
+        case MSCORE_ACTION_CTRL_O:      uart_puts(handle, "[Ctrl+O]");      break;
+        case MSCORE_ACTION_CTRL_P:      uart_puts(handle, "[Ctrl+P]");      break;
+        case MSCORE_ACTION_CTRL_Q:      uart_puts(handle, "[Ctrl+Q]");      break;
+        case MSCORE_ACTION_CTRL_R:      uart_puts(handle, "[Ctrl+R]");      break;
+        case MSCORE_ACTION_CTRL_S:      uart_puts(handle, "[Ctrl+S]");      break;
+        case MSCORE_ACTION_CTRL_T:      uart_puts(handle, "[Ctrl+T]");      break;
+        case MSCORE_ACTION_CTRL_U:      uart_puts(handle, "[Ctrl+U]");      break;
+        case MSCORE_ACTION_CTRL_V:      uart_puts(handle, "[Ctrl+V]");      break;
+        case MSCORE_ACTION_CTRL_W:      uart_puts(handle, "[Ctrl+W]");      break;
+        case MSCORE_ACTION_CTRL_X:      uart_puts(handle, "[Ctrl+X]");      break;
+        case MSCORE_ACTION_CTRL_Y:      uart_puts(handle, "[Ctrl+Y]");      break;
+        case MSCORE_ACTION_CTRL_Z:      uart_puts(handle, "[Ctrl+Z]");      break;
+        case MSCORE_ACTION_F1:          uart_puts(handle, "[F1]");          break;
+        case MSCORE_ACTION_F2:          uart_puts(handle, "[F2]");          break;
+        case MSCORE_ACTION_F3:          uart_puts(handle, "[F3]");          break;
+        case MSCORE_ACTION_F4:          uart_puts(handle, "[F4]");          break;
+        case MSCORE_ACTION_F5:          uart_puts(handle, "[F5]");          break;
+        case MSCORE_ACTION_F6:          uart_puts(handle, "[F6]");          break;
+        case MSCORE_ACTION_F7:          uart_puts(handle, "[F7]");          break;
+        case MSCORE_ACTION_F8:          uart_puts(handle, "[F8]");          break;
+        case MSCORE_ACTION_F9:          uart_puts(handle, "[F9]");          break;
+        case MSCORE_ACTION_F10:         uart_puts(handle, "[F10]");         break;
+        case MSCORE_ACTION_F11:         uart_puts(handle, "[F11]");         break;
+        case MSCORE_ACTION_F12:         uart_puts(handle, "[F12]");         break;
+        case MSCORE_ACTION_ARROW_LEFT:  uart_puts(handle, "[<]");           break;
+        case MSCORE_ACTION_ARROW_RIGHT: uart_puts(handle, "[>]");           break;
+        case MSCORE_ACTION_ARROW_UP:    uart_puts(handle, "[UP]");          break;
+        case MSCORE_ACTION_ARROW_DOWN:  uart_puts(handle, "[DOWN]");        break;
+        case MSCORE_ACTION_HOME:        uart_puts(handle, "[HOME]");        break;
+        case MSCORE_ACTION_END:         uart_puts(handle, "[END]");         break;
+        case MSCORE_ACTION_PAGE_UP:     uart_puts(handle, "[PAGE UP]");     break;
+        case MSCORE_ACTION_PAGE_DOWN:   uart_puts(handle, "[PAGE DOWN]");   break;
+    }
+}
+
+#endif
+
+#if PRINT_CODE_ENABLED
+
+static void print_code(MICROSHELL *handle, char c)
+{
+    static const char *txt = "0123456789ABCDEF";
+    handle->uart_putc('[');
+    handle->uart_putc('0');
+    handle->uart_putc('x');
+    handle->uart_putc(txt[(((unsigned char)c) >> 4) & 0x0F]);
+    handle->uart_putc(txt[(((unsigned char)c) >> 0) & 0x0F]);
+    handle->uart_putc(']');
+}
+
+#endif
+
+static char *copy_forward(char *buf, int ofs_src, int ofs_des, int siz)
+{
+    int i;
+    char *p_src = buf + ofs_src;
+    char *p_des = buf + ofs_des;
+    for (i = 0; i < siz; i++) {
+        *p_des++ = *p_src++;
+    }
+    return buf;
+}
+
+static char *copy_backward(char *buf, int ofs_src, int ofs_des, int siz)
+{
+    int i;
+    char *p_src = buf + ofs_src + siz;
+    char *p_des = buf + ofs_des + siz;
+    for (i = 0; i < siz; i++) {
+        *p_des-- = *p_src--;
+    }
+    return buf;
+}
+
+static char *clean_buffer(char *buf, int siz)
+{
+    int i;
+    char *p = buf;
+    for (i = 0; i < siz; i++) {
+        *p++ = 0;
+    }
+    return buf;
+}
+
+static int print_buffer(MICROSHELL *handle, char *buf, int ofs)
+{
+    int cnt = 0;
+    char *p = buf + ofs;
+    while (*p) {
+        handle->uart_putc(*p++);
+        cnt++;
+    }
+    return cnt;
+}
+
+static int print_char(MICROSHELL *handle, char c)
+{
+    handle->uart_putc(c);
+    return 1;
+}
+
+static void print_return(MICROSHELL *handle)
+{
+    handle->uart_putc('\r');
+    handle->uart_putc('\n');
+}
+
+static void cursor_left(MICROSHELL *handle, int n)
+{
+    int i;
+    for (i = 0; i < n; i++) {
+        handle->uart_putc('\x1B');
+        handle->uart_putc('[');
+        handle->uart_putc('D');
+    }
+}
+
+static void cursor_right(MICROSHELL *handle, int n)
+{
+    int i;
+    for (i = 0; i < n; i++) {
+        handle->uart_putc('\x1B');
+        handle->uart_putc('[');
+        handle->uart_putc('C');
+    }
+}
+
+void microshell_init(MICROSHELL *handle, MICROSHELL_UART_PUTC uart_putc, MICROSHELL_UART_GETC uart_getc, MICROSHELL_ACTION_HOOK action_hook)
+{
+    mscore_init(&(handle->mscore));
+    handle->uart_putc = uart_putc;
+    handle->uart_getc = uart_getc;
+    handle->action_hook = action_hook;
+}
+
+char *microshell_getline(MICROSHELL *handle, char *buf, int siz)
+{
+    char *ptr = buf;
+    int pos = 0;
+    int len = 0;
+    clean_buffer(buf, siz);
+    while (1) {
+        char c = handle->uart_getc();
+        MSCORE_ACTION action = mscore_push(&(handle->mscore), c);
+        if (handle->action_hook) {
+            handle->action_hook(action);
+        }
+#if PRINT_ACTION_ENABLED
+        print_action(handle, action);
+#endif
+#if PRINT_CODE_ENABLED
+        print_code(handle, c);
+#endif
+        switch (action) {
+            case MSCORE_ACTION_DISPLAYABLE:
+                {
+                    int n = 0;
+                    copy_backward(buf, pos - 1, pos - 0, len - pos + 1);
+                    handle->uart_putc(c);
+                    *ptr++ = c;
+                    pos++;
+                    len++;
+                    n += print_buffer(handle, buf, pos);
+                    cursor_left(handle, n);
+                    if (len >= siz - 1) {
+                        print_return(handle);
+                        goto end;
+                    }
+                }
+                break;
+            case MSCORE_ACTION_ENTER:
+            case MSCORE_ACTION_CTRL_J:
+                print_return(handle);
+                goto end;
+                break;
+            case MSCORE_ACTION_CTRL_C:
+                clean_buffer(buf, siz);
+                print_char(handle, '^');
+                print_char(handle, 'C');
+                print_return(handle);
+                goto end;
+                break;
+            case MSCORE_ACTION_BS:
+            case MSCORE_ACTION_CTRL_H:
+                if (pos > 0) {
+                    int n = 0;
+                    copy_forward(buf, pos, pos - 1, len - pos + 1);
+                    ptr--;
+                    pos--;
+                    len--;
+                    cursor_left(handle, 1);
+                    n += print_buffer(handle, buf, pos);
+                    n += print_char(handle, ' ');
+                    cursor_left(handle, n);
+                }
+                break;
+            case MSCORE_ACTION_DEL:
+            case MSCORE_ACTION_CTRL_D:
+                if (len > 0) {
+                    int n = 0;
+                    copy_forward(buf, pos + 1, pos + 0, len - pos + 1);
+                    len--;
+                    n += print_buffer(handle, buf, pos);
+                    n += print_char(handle, ' ');
+                    cursor_left(handle, n);
+                }
+                break;
+            case MSCORE_ACTION_ARROW_LEFT:
+            case MSCORE_ACTION_CTRL_B:
+                if (pos > 0) {
+                    ptr--;
+                    pos--;
+                    cursor_left(handle, 1);
+                }
+                break;
+            case MSCORE_ACTION_ARROW_RIGHT:
+            case MSCORE_ACTION_CTRL_F:
+                if (pos < len) {
+                    ptr++;
+                    pos++;
+                    cursor_right(handle, 1);
+                }
+                break;
+            case MSCORE_ACTION_HOME:
+            case MSCORE_ACTION_CTRL_A:
+                if (pos > 0) {
+                    int n = pos;
+                    ptr -= n;
+                    pos = 0;
+                    cursor_left(handle, n);
+                }
+                break;
+            case MSCORE_ACTION_END:
+            case MSCORE_ACTION_CTRL_E:
+                if (pos < len) {
+                    int n = len - pos;
+                    ptr += n;
+                    pos = len;
+                    cursor_right(handle, n);
+                }
+                break;
+            default:
+                break;
+        }
+    }
+
+end:
+    return buf;
+}
+
diff -r 000000000000 -r ff53d8f5146a core/microshell.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/core/microshell.h	Thu Oct 19 07:39:57 2017 +0000
@@ -0,0 +1,62 @@
+/**
+ * @file      microshell.h
+ * @author    Shinichiro Nakamura (CuBeatSystems)
+ * ===============================================================
+ * MicroShell (Version 0.0.2)
+ * Copyright (c) 2016, 2017 Shinichiro Nakamura (CuBeatSystems)
+ * ===============================================================
+ * The MIT License : https://opensource.org/licenses/MIT
+ *
+ * Copyright (c) 2016, 2017 Shinichiro Nakamura (CuBeatSystems)
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef MICROSHELL_H
+#define MICROSHELL_H
+
+#include "mscore.h"
+
+typedef void (*MICROSHELL_UART_PUTC)(char c);
+typedef char (*MICROSHELL_UART_GETC)(void);
+typedef void (*MICROSHELL_ACTION_HOOK)(MSCORE_ACTION action);
+
+typedef struct {
+    MSCORE mscore;
+    MICROSHELL_UART_PUTC uart_putc;
+    MICROSHELL_UART_GETC uart_getc;
+    MICROSHELL_ACTION_HOOK action_hook;
+} MICROSHELL;
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void microshell_init(MICROSHELL *handle, MICROSHELL_UART_PUTC uart_putc, MICROSHELL_UART_GETC uart_getc, MICROSHELL_ACTION_HOOK action_hook);
+char *microshell_getline(MICROSHELL *handle, char *buf, int siz);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
+
diff -r 000000000000 -r ff53d8f5146a core/msconf.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/core/msconf.h	Thu Oct 19 07:39:57 2017 +0000
@@ -0,0 +1,51 @@
+/**
+ * @file      msconf.h
+ * @author    Shinichiro Nakamura (CuBeatSystems)
+ * ===============================================================
+ * MicroShell (Version 0.0.2)
+ * Copyright (c) 2016, 2017 Shinichiro Nakamura (CuBeatSystems)
+ * ===============================================================
+ * The MIT License : https://opensource.org/licenses/MIT
+ *
+ * Copyright (c) 2016, 2017 Shinichiro Nakamura (CuBeatSystems)
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef MSCONF_H
+#define MSCONF_H
+
+#define MSCONF_MAX_INPUT_LENGTH     (32)
+#define MSCONF_MAX_INPUT_ARGS       ((MSCONF_MAX_INPUT_LENGTH / 2) + 1)
+
+#if !defined(MSCONF_KEYMAP_SWAP_BS_DEL)
+#define MSCONF_KEYMAP_SWAP_BS_DEL   (0)
+#endif
+
+#define MSCONF_KEYMAP_USE_CTRL      (1)
+#define MSCONF_KEYMAP_USE_FUNCTION  (1)
+#define MSCONF_KEYMAP_USE_ARROW     (1)
+#define MSCONF_KEYMAP_USE_HOME_END  (1)
+#define MSCONF_KEYMAP_USE_PAGE      (1)
+
+#endif
+
diff -r 000000000000 -r ff53d8f5146a core/mscore.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/core/mscore.c	Thu Oct 19 07:39:57 2017 +0000
@@ -0,0 +1,193 @@
+/**
+ * @file      mscore.c
+ * @author    Shinichiro Nakamura (CuBeatSystems)
+ * ===============================================================
+ * MicroShell (Version 0.0.2)
+ * Copyright (c) 2016, 2017 Shinichiro Nakamura (CuBeatSystems)
+ * ===============================================================
+ * The MIT License : https://opensource.org/licenses/MIT
+ *
+ * Copyright (c) 2016, 2017 Shinichiro Nakamura (CuBeatSystems)
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include "mscore.h"
+#include "msconf.h"
+
+typedef char * MSCORE_STREAM;
+
+typedef struct {
+    MSCORE_STREAM stream;
+    MSCORE_ACTION action;
+} MSCORE_KEYMAP;
+
+static const MSCORE_KEYMAP kmlist[] = {
+#if MSCONF_KEYMAP_SWAP_BS_DEL
+    {   "\x7f",                 MSCORE_ACTION_BS            },
+    {   "\x1b\x5b\x33\x7e",     MSCORE_ACTION_DEL           },
+#else
+    {   "\x08",                 MSCORE_ACTION_BS            },
+    {   "\x7f",                 MSCORE_ACTION_DEL           },
+#endif
+    {   "\x09",                 MSCORE_ACTION_TAB           },
+    {   "\x1b\x5b\x32\x7e",     MSCORE_ACTION_INSERT        },
+    {   "\x0d",                 MSCORE_ACTION_ENTER         },
+#if MSCONF_KEYMAP_USE_CTRL
+    {   "\x01",                 MSCORE_ACTION_CTRL_A        },
+    {   "\x02",                 MSCORE_ACTION_CTRL_B        },
+    {   "\x03",                 MSCORE_ACTION_CTRL_C        },
+    {   "\x04",                 MSCORE_ACTION_CTRL_D        },
+    {   "\x05",                 MSCORE_ACTION_CTRL_E        },
+    {   "\x06",                 MSCORE_ACTION_CTRL_F        },
+    {   "\x07",                 MSCORE_ACTION_CTRL_G        },
+    {   "\x08",                 MSCORE_ACTION_CTRL_H        },
+    {   "\x09",                 MSCORE_ACTION_CTRL_I        },
+    {   "\x0a",                 MSCORE_ACTION_CTRL_J        },
+    {   "\x0b",                 MSCORE_ACTION_CTRL_K        },
+    {   "\x0c",                 MSCORE_ACTION_CTRL_L        },
+    {   "\x0d",                 MSCORE_ACTION_CTRL_M        },
+    {   "\x0e",                 MSCORE_ACTION_CTRL_N        },
+    {   "\x0f",                 MSCORE_ACTION_CTRL_O        },
+    {   "\x10",                 MSCORE_ACTION_CTRL_P        },
+    {   "\x11",                 MSCORE_ACTION_CTRL_Q        },
+    {   "\x12",                 MSCORE_ACTION_CTRL_R        },
+    {   "\x13",                 MSCORE_ACTION_CTRL_S        },
+    {   "\x14",                 MSCORE_ACTION_CTRL_T        },
+    {   "\x15",                 MSCORE_ACTION_CTRL_U        },
+    {   "\x16",                 MSCORE_ACTION_CTRL_V        },
+    {   "\x17",                 MSCORE_ACTION_CTRL_W        },
+    {   "\x18",                 MSCORE_ACTION_CTRL_X        },
+    {   "\x19",                 MSCORE_ACTION_CTRL_Y        },
+    {   "\x1a",                 MSCORE_ACTION_CTRL_Z        },
+#endif
+#if MSCONF_KEYMAP_USE_FUNCTION
+    {   "\x1b\x5b\x31\x31\x7e", MSCORE_ACTION_F1            },
+    {   "\x1b\x5b\x31\x32\x7e", MSCORE_ACTION_F2            },
+    {   "\x1b\x5b\x31\x33\x7e", MSCORE_ACTION_F3            },
+    {   "\x1b\x5b\x31\x34\x7e", MSCORE_ACTION_F4            },
+    {   "\x1b\x5b\x31\x35\x7e", MSCORE_ACTION_F5            },
+    {   "\x1b\x5b\x31\x37\x7e", MSCORE_ACTION_F6            },
+    {   "\x1b\x5b\x31\x38\x7e", MSCORE_ACTION_F7            },
+    {   "\x1b\x5b\x31\x39\x7e", MSCORE_ACTION_F8            },
+    {   "\x1b\x5b\x32\x30\x7e", MSCORE_ACTION_F9            },
+    {   "\x1b\x5b\x32\x31\x7e", MSCORE_ACTION_F10           },
+    {   "\x1b\x5b\x32\x32\x7e", MSCORE_ACTION_F11           },
+    {   "\x1b\x5b\x32\x33\x7e", MSCORE_ACTION_F12           },
+    {   "\x1b\x4f\x50",         MSCORE_ACTION_F1            },
+    {   "\x1b\x4f\x51",         MSCORE_ACTION_F2            },
+    {   "\x1b\x4f\x52",         MSCORE_ACTION_F3            },
+    {   "\x1b\x4f\x53",         MSCORE_ACTION_F4            },
+    {   "\x1b\x5b\x31\x35\x7e", MSCORE_ACTION_F5            },
+    {   "\x1b\x5b\x31\x37\x7e", MSCORE_ACTION_F6            },
+    {   "\x1b\x5b\x31\x38\x7e", MSCORE_ACTION_F7            },
+    {   "\x1b\x5b\x31\x39\x7e", MSCORE_ACTION_F8            },
+    {   "\x1b\x5b\x32\x31\x7e", MSCORE_ACTION_F9            },
+    {   "\x1b\x5b\x32\x32\x7e", MSCORE_ACTION_F10           },
+    {   "\x1b\x5b\x32\x33\x7e", MSCORE_ACTION_F11           },
+    {   "\x1b\x5b\x32\x34\x7e", MSCORE_ACTION_F12           },
+#endif
+#if MSCONF_KEYMAP_USE_ARROW
+    {   "\x1b\x5b\x41",         MSCORE_ACTION_ARROW_UP      },
+    {   "\x1b\x5b\x42",         MSCORE_ACTION_ARROW_DOWN    },
+    {   "\x1b\x5b\x43",         MSCORE_ACTION_ARROW_RIGHT   },
+    {   "\x1b\x5b\x44",         MSCORE_ACTION_ARROW_LEFT    },
+#endif
+#if MSCONF_KEYMAP_USE_HOME_END
+    {   "\x1b\x4f\x48",         MSCORE_ACTION_HOME          },
+    {   "\x1b\x4f\x46",         MSCORE_ACTION_END           },
+    {   "\x1b[1~",              MSCORE_ACTION_HOME          },
+    {   "\x1b[4~",              MSCORE_ACTION_END           },
+#endif
+#if MSCONF_KEYMAP_USE_PAGE
+    {   "\x1b\x5b\x35\x7e",     MSCORE_ACTION_PAGE_UP       },
+    {   "\x1b\x5b\x36\x7e",     MSCORE_ACTION_PAGE_DOWN     },
+#endif
+};
+
+static int buf_length(char *buf)
+{
+    int n = 0;
+    while (*buf++) {
+        n++;
+    }
+    return n;
+}
+
+static int buf_match(char *a, char *b, int n)
+{
+    int i;
+    for (i = 0; i < n; i++) {
+        if (*a++ != *b++) {
+            return 0;
+        }
+    }
+    return 1;
+}
+
+static int buf_clear(char *buf, int n)
+{
+    int i;
+    for (i = 0; i < n; i++) {
+        buf[i] = 0;
+    }
+    return 1;
+}
+
+void mscore_init(MSCORE *handle)
+{
+    buf_clear(handle->keybuf, MSCORE_BUFFER_SIZE);
+    handle->keycnt = 0;
+}
+
+MSCORE_ACTION mscore_push(MSCORE *handle, char c)
+{
+    int match = 0;
+    handle->keybuf[handle->keycnt++] = c;
+    const MSCORE_KEYMAP *p = &kmlist[0];
+    const int N = sizeof(kmlist) / sizeof(kmlist[0]);
+    int i;
+    for (i = 0; i < N; i++) {
+        if (handle->keycnt == buf_length(p->stream))  {
+            if (buf_match(p->stream, handle->keybuf, handle->keycnt)) {
+                handle->keycnt = 0;
+                buf_clear(handle->keybuf, MSCORE_BUFFER_SIZE);
+                return p->action;
+            }
+        } else {
+            if (buf_match(p->stream, handle->keybuf, handle->keycnt)) {
+                match = 1;
+                break;
+            }
+        }
+        p++;
+    }
+    if (!match) {
+        handle->keycnt = 0;
+        buf_clear(handle->keybuf, MSCORE_BUFFER_SIZE);
+        if ((' ' <= c) && (c <= '~')) {
+            return MSCORE_ACTION_DISPLAYABLE;
+        }
+    }
+    return MSCORE_ACTION_IGNORE;
+}
+
diff -r 000000000000 -r ff53d8f5146a core/mscore.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/core/mscore.h	Thu Oct 19 07:39:57 2017 +0000
@@ -0,0 +1,113 @@
+/**
+ * @file      mscore.h
+ * @author    Shinichiro Nakamura (CuBeatSystems)
+ * ===============================================================
+ * MicroShell (Version 0.0.2)
+ * Copyright (c) 2016, 2017 Shinichiro Nakamura (CuBeatSystems)
+ * ===============================================================
+ * The MIT License : https://opensource.org/licenses/MIT
+ *
+ * Copyright (c) 2016, 2017 Shinichiro Nakamura (CuBeatSystems)
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef MSCORE_H
+#define MSCORE_H
+
+#define MSCORE_BUFFER_SIZE     (8)
+#define MSCORE_INITIALIZER()   { .keycnt = 0, .keybuf = {0} }
+
+typedef enum {
+    MSCORE_ACTION_IGNORE,
+    MSCORE_ACTION_DISPLAYABLE,
+    MSCORE_ACTION_BS,
+    MSCORE_ACTION_DEL,
+    MSCORE_ACTION_TAB,
+    MSCORE_ACTION_INSERT,
+    MSCORE_ACTION_ENTER,
+    MSCORE_ACTION_CTRL_A,
+    MSCORE_ACTION_CTRL_B,
+    MSCORE_ACTION_CTRL_C,
+    MSCORE_ACTION_CTRL_D,
+    MSCORE_ACTION_CTRL_E,
+    MSCORE_ACTION_CTRL_F,
+    MSCORE_ACTION_CTRL_G,
+    MSCORE_ACTION_CTRL_H,
+    MSCORE_ACTION_CTRL_I,
+    MSCORE_ACTION_CTRL_J,
+    MSCORE_ACTION_CTRL_K,
+    MSCORE_ACTION_CTRL_L,
+    MSCORE_ACTION_CTRL_M,
+    MSCORE_ACTION_CTRL_N,
+    MSCORE_ACTION_CTRL_O,
+    MSCORE_ACTION_CTRL_P,
+    MSCORE_ACTION_CTRL_Q,
+    MSCORE_ACTION_CTRL_R,
+    MSCORE_ACTION_CTRL_S,
+    MSCORE_ACTION_CTRL_T,
+    MSCORE_ACTION_CTRL_U,
+    MSCORE_ACTION_CTRL_V,
+    MSCORE_ACTION_CTRL_W,
+    MSCORE_ACTION_CTRL_X,
+    MSCORE_ACTION_CTRL_Y,
+    MSCORE_ACTION_CTRL_Z,
+    MSCORE_ACTION_F1,
+    MSCORE_ACTION_F2,
+    MSCORE_ACTION_F3,
+    MSCORE_ACTION_F4,
+    MSCORE_ACTION_F5,
+    MSCORE_ACTION_F6,
+    MSCORE_ACTION_F7,
+    MSCORE_ACTION_F8,
+    MSCORE_ACTION_F9,
+    MSCORE_ACTION_F10,
+    MSCORE_ACTION_F11,
+    MSCORE_ACTION_F12,
+    MSCORE_ACTION_ARROW_UP,
+    MSCORE_ACTION_ARROW_DOWN,
+    MSCORE_ACTION_ARROW_LEFT,
+    MSCORE_ACTION_ARROW_RIGHT,
+    MSCORE_ACTION_HOME,
+    MSCORE_ACTION_END,
+    MSCORE_ACTION_PAGE_UP,
+    MSCORE_ACTION_PAGE_DOWN,
+} MSCORE_ACTION;
+
+typedef struct {
+    int keycnt;
+    char keybuf[MSCORE_BUFFER_SIZE];
+} MSCORE;
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void mscore_init(MSCORE *handle);
+MSCORE_ACTION mscore_push(MSCORE *handle, char c);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
+