Have you tried to develop a key input feature on your small embedded platform with a serial input? It is not so easy to implement correctly because the input methodology is using a complicated protocol known as VT100. In my recent project "Natural Tiny Shell (NT-Shell)" provides VT100 compatible terminal control features for small embedded systems. However, the middleware is still large for small embedded processors such as 8-bit MCUs, 16-bit MCUs and also for small 32-bit MCUs like Cortex-M0. This "MicroShell" middleware provides a minimal terminal control for the platforms.

Dependents:   MicroShellExample

Committer:
shintamainjp
Date:
Sun Feb 05 03:55:01 2017 +0000
Revision:
0:e91b984e285d
first commitment.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shintamainjp 0:e91b984e285d 1 /**
shintamainjp 0:e91b984e285d 2 * @file ntlibc.c
shintamainjp 0:e91b984e285d 3 * @author Shinichiro Nakamura (CuBeatSystems)
shintamainjp 0:e91b984e285d 4 * ===============================================================
shintamainjp 0:e91b984e285d 5 * MicroShell (Version 0.0.1)
shintamainjp 0:e91b984e285d 6 * Copyright (c) 2016, 2017 Shinichiro Nakamura (CuBeatSystems)
shintamainjp 0:e91b984e285d 7 * ===============================================================
shintamainjp 0:e91b984e285d 8 * The MIT License : https://opensource.org/licenses/MIT
shintamainjp 0:e91b984e285d 9 *
shintamainjp 0:e91b984e285d 10 * Copyright (c) 2016, 2017 Shinichiro Nakamura (CuBeatSystems)
shintamainjp 0:e91b984e285d 11 *
shintamainjp 0:e91b984e285d 12 * Permission is hereby granted, free of charge, to any person
shintamainjp 0:e91b984e285d 13 * obtaining a copy of this software and associated documentation
shintamainjp 0:e91b984e285d 14 * files (the "Software"), to deal in the Software without
shintamainjp 0:e91b984e285d 15 * restriction, including without limitation the rights to use,
shintamainjp 0:e91b984e285d 16 * copy, modify, merge, publish, distribute, sublicense, and/or
shintamainjp 0:e91b984e285d 17 * sell copies of the Software, and to permit persons to whom the
shintamainjp 0:e91b984e285d 18 * Software is furnished to do so, subject to the following
shintamainjp 0:e91b984e285d 19 * conditions:
shintamainjp 0:e91b984e285d 20 *
shintamainjp 0:e91b984e285d 21 * The above copyright notice and this permission notice shall be
shintamainjp 0:e91b984e285d 22 * included in all copies or substantial portions of the Software.
shintamainjp 0:e91b984e285d 23 *
shintamainjp 0:e91b984e285d 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
shintamainjp 0:e91b984e285d 25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
shintamainjp 0:e91b984e285d 26 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
shintamainjp 0:e91b984e285d 27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
shintamainjp 0:e91b984e285d 28 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
shintamainjp 0:e91b984e285d 29 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
shintamainjp 0:e91b984e285d 30 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
shintamainjp 0:e91b984e285d 31 * OTHER DEALINGS IN THE SOFTWARE.
shintamainjp 0:e91b984e285d 32 */
shintamainjp 0:e91b984e285d 33
shintamainjp 0:e91b984e285d 34 #include "ntlibc.h"
shintamainjp 0:e91b984e285d 35
shintamainjp 0:e91b984e285d 36 int ntlibc_strlen(const char *s)
shintamainjp 0:e91b984e285d 37 {
shintamainjp 0:e91b984e285d 38 const char *p = s;
shintamainjp 0:e91b984e285d 39 int cnt = 0;
shintamainjp 0:e91b984e285d 40 while (*p) {
shintamainjp 0:e91b984e285d 41 cnt++;
shintamainjp 0:e91b984e285d 42 p++;
shintamainjp 0:e91b984e285d 43 }
shintamainjp 0:e91b984e285d 44 return cnt;
shintamainjp 0:e91b984e285d 45 }
shintamainjp 0:e91b984e285d 46
shintamainjp 0:e91b984e285d 47 char *ntlibc_strcpy(char *des, const char *src)
shintamainjp 0:e91b984e285d 48 {
shintamainjp 0:e91b984e285d 49 char *d = des;
shintamainjp 0:e91b984e285d 50 const char *s = src;
shintamainjp 0:e91b984e285d 51 while (*s) {
shintamainjp 0:e91b984e285d 52 *d = *s;
shintamainjp 0:e91b984e285d 53 d++;
shintamainjp 0:e91b984e285d 54 s++;
shintamainjp 0:e91b984e285d 55 }
shintamainjp 0:e91b984e285d 56 *d = '\0';
shintamainjp 0:e91b984e285d 57 return des;
shintamainjp 0:e91b984e285d 58 }
shintamainjp 0:e91b984e285d 59
shintamainjp 0:e91b984e285d 60 char *ntlibc_strcat(char *des, const char *src)
shintamainjp 0:e91b984e285d 61 {
shintamainjp 0:e91b984e285d 62 char *d = des;
shintamainjp 0:e91b984e285d 63 const char *s = src;
shintamainjp 0:e91b984e285d 64 while (*d) {
shintamainjp 0:e91b984e285d 65 d++;
shintamainjp 0:e91b984e285d 66 }
shintamainjp 0:e91b984e285d 67 while (*s) {
shintamainjp 0:e91b984e285d 68 *d = *s;
shintamainjp 0:e91b984e285d 69 d++;
shintamainjp 0:e91b984e285d 70 s++;
shintamainjp 0:e91b984e285d 71 }
shintamainjp 0:e91b984e285d 72 *d = '\0';
shintamainjp 0:e91b984e285d 73 return des;
shintamainjp 0:e91b984e285d 74 }
shintamainjp 0:e91b984e285d 75
shintamainjp 0:e91b984e285d 76 int ntlibc_strcmp(const char *s1, const char *s2)
shintamainjp 0:e91b984e285d 77 {
shintamainjp 0:e91b984e285d 78 char *p1 = (char *)s1;
shintamainjp 0:e91b984e285d 79 char *p2 = (char *)s2;
shintamainjp 0:e91b984e285d 80 while (*p1 || *p2) {
shintamainjp 0:e91b984e285d 81 if (*p1 != *p2) {
shintamainjp 0:e91b984e285d 82 return (*p1 < *p2) ? -1 : 1;
shintamainjp 0:e91b984e285d 83 }
shintamainjp 0:e91b984e285d 84 p1++;
shintamainjp 0:e91b984e285d 85 p2++;
shintamainjp 0:e91b984e285d 86 }
shintamainjp 0:e91b984e285d 87 if (*p1 == *p2) {
shintamainjp 0:e91b984e285d 88 return 0;
shintamainjp 0:e91b984e285d 89 } else {
shintamainjp 0:e91b984e285d 90 return (*p1 < *p2) ? -1 : 1;
shintamainjp 0:e91b984e285d 91 }
shintamainjp 0:e91b984e285d 92 }
shintamainjp 0:e91b984e285d 93
shintamainjp 0:e91b984e285d 94 int ntlibc_stricmp(const char *s1, const char *s2)
shintamainjp 0:e91b984e285d 95 {
shintamainjp 0:e91b984e285d 96 char *p1 = (char *)s1;
shintamainjp 0:e91b984e285d 97 char *p2 = (char *)s2;
shintamainjp 0:e91b984e285d 98 while (*p1 || *p2) {
shintamainjp 0:e91b984e285d 99 if (ntlibc_toupper(*p1) != ntlibc_toupper(*p2)) {
shintamainjp 0:e91b984e285d 100 return (*p1 < *p2) ? -1 : 1;
shintamainjp 0:e91b984e285d 101 }
shintamainjp 0:e91b984e285d 102 p1++;
shintamainjp 0:e91b984e285d 103 p2++;
shintamainjp 0:e91b984e285d 104 }
shintamainjp 0:e91b984e285d 105 if (*p1 == *p2) {
shintamainjp 0:e91b984e285d 106 return 0;
shintamainjp 0:e91b984e285d 107 } else {
shintamainjp 0:e91b984e285d 108 return (*p1 < *p2) ? -1 : 1;
shintamainjp 0:e91b984e285d 109 }
shintamainjp 0:e91b984e285d 110 }
shintamainjp 0:e91b984e285d 111
shintamainjp 0:e91b984e285d 112 int ntlibc_strncmp(const char *s1, const char *s2, int n)
shintamainjp 0:e91b984e285d 113 {
shintamainjp 0:e91b984e285d 114 char *p1 = (char *)s1;
shintamainjp 0:e91b984e285d 115 char *p2 = (char *)s2;
shintamainjp 0:e91b984e285d 116 int len = 0;
shintamainjp 0:e91b984e285d 117 while (*p1 || *p2) {
shintamainjp 0:e91b984e285d 118 if (n <= len) {
shintamainjp 0:e91b984e285d 119 break;
shintamainjp 0:e91b984e285d 120 }
shintamainjp 0:e91b984e285d 121 if (*p1 != *p2) {
shintamainjp 0:e91b984e285d 122 return (*p1 < *p2) ? -1 : 1;
shintamainjp 0:e91b984e285d 123 }
shintamainjp 0:e91b984e285d 124 p1++;
shintamainjp 0:e91b984e285d 125 p2++;
shintamainjp 0:e91b984e285d 126 len++;
shintamainjp 0:e91b984e285d 127 }
shintamainjp 0:e91b984e285d 128 return 0;
shintamainjp 0:e91b984e285d 129 }
shintamainjp 0:e91b984e285d 130
shintamainjp 0:e91b984e285d 131 int ntlibc_isdigit(int c)
shintamainjp 0:e91b984e285d 132 {
shintamainjp 0:e91b984e285d 133 if (('0' <= c) && (c <= '9')) {
shintamainjp 0:e91b984e285d 134 return 1;
shintamainjp 0:e91b984e285d 135 }
shintamainjp 0:e91b984e285d 136 return 0;
shintamainjp 0:e91b984e285d 137 }
shintamainjp 0:e91b984e285d 138
shintamainjp 0:e91b984e285d 139 int ntlibc_isalpha(int c)
shintamainjp 0:e91b984e285d 140 {
shintamainjp 0:e91b984e285d 141 if (('A' <= c) && (c <= 'Z')) {
shintamainjp 0:e91b984e285d 142 return 1;
shintamainjp 0:e91b984e285d 143 }
shintamainjp 0:e91b984e285d 144 if (('a' <= c) && (c <= 'z')) {
shintamainjp 0:e91b984e285d 145 return 1;
shintamainjp 0:e91b984e285d 146 }
shintamainjp 0:e91b984e285d 147 return 0;
shintamainjp 0:e91b984e285d 148 }
shintamainjp 0:e91b984e285d 149
shintamainjp 0:e91b984e285d 150 int ntlibc_iscntrl(int c)
shintamainjp 0:e91b984e285d 151 {
shintamainjp 0:e91b984e285d 152 if (c == 0x07) { return 0; }
shintamainjp 0:e91b984e285d 153 if (c == 0x08) { return 0; }
shintamainjp 0:e91b984e285d 154 if (c == 0x09) { return 0; }
shintamainjp 0:e91b984e285d 155 if (c == 0x0a) { return 0; }
shintamainjp 0:e91b984e285d 156 if (c == 0x0b) { return 0; }
shintamainjp 0:e91b984e285d 157 if (c == 0x0c) { return 0; }
shintamainjp 0:e91b984e285d 158 if (c == 0x0d) { return 0; }
shintamainjp 0:e91b984e285d 159 if ((0x00 <= c) && (c <= 0x1f)) {
shintamainjp 0:e91b984e285d 160 return 1;
shintamainjp 0:e91b984e285d 161 }
shintamainjp 0:e91b984e285d 162 return 0;
shintamainjp 0:e91b984e285d 163 }
shintamainjp 0:e91b984e285d 164
shintamainjp 0:e91b984e285d 165 int ntlibc_toupper(int c)
shintamainjp 0:e91b984e285d 166 {
shintamainjp 0:e91b984e285d 167 if (('a' <= c) && (c <= 'z')) {
shintamainjp 0:e91b984e285d 168 int diff = 'a' - 'A';
shintamainjp 0:e91b984e285d 169 return c - diff;
shintamainjp 0:e91b984e285d 170 }
shintamainjp 0:e91b984e285d 171 return c;
shintamainjp 0:e91b984e285d 172 }
shintamainjp 0:e91b984e285d 173
shintamainjp 0:e91b984e285d 174 int ntlibc_tolower(int c)
shintamainjp 0:e91b984e285d 175 {
shintamainjp 0:e91b984e285d 176 if (('A' <= c) && (c <= 'Z')) {
shintamainjp 0:e91b984e285d 177 int diff = 'a' - 'A';
shintamainjp 0:e91b984e285d 178 return c + diff;
shintamainjp 0:e91b984e285d 179 }
shintamainjp 0:e91b984e285d 180 return c;
shintamainjp 0:e91b984e285d 181 }
shintamainjp 0:e91b984e285d 182
shintamainjp 0:e91b984e285d 183 int ntlibc_atoi(const char *nptr)
shintamainjp 0:e91b984e285d 184 {
shintamainjp 0:e91b984e285d 185 int cnt;
shintamainjp 0:e91b984e285d 186 int num = 0;
shintamainjp 0:e91b984e285d 187 int ofs = 0;
shintamainjp 0:e91b984e285d 188 int sign = 0;
shintamainjp 0:e91b984e285d 189 int scnt = 0;
shintamainjp 0:e91b984e285d 190 char *p = (char *)nptr;
shintamainjp 0:e91b984e285d 191 while (*p != '\0') {
shintamainjp 0:e91b984e285d 192 if (!ntlibc_isdigit(*p)) {
shintamainjp 0:e91b984e285d 193 if (*p == ' ') {
shintamainjp 0:e91b984e285d 194 ofs++;
shintamainjp 0:e91b984e285d 195 }
shintamainjp 0:e91b984e285d 196 if (*p == '+') {
shintamainjp 0:e91b984e285d 197 sign = 0;
shintamainjp 0:e91b984e285d 198 ofs++;
shintamainjp 0:e91b984e285d 199 if (scnt++ > 0) {
shintamainjp 0:e91b984e285d 200 return 0;
shintamainjp 0:e91b984e285d 201 }
shintamainjp 0:e91b984e285d 202 }
shintamainjp 0:e91b984e285d 203 if (*p == '-') {
shintamainjp 0:e91b984e285d 204 sign = 1;
shintamainjp 0:e91b984e285d 205 ofs++;
shintamainjp 0:e91b984e285d 206 if (scnt++ > 0) {
shintamainjp 0:e91b984e285d 207 return 0;
shintamainjp 0:e91b984e285d 208 }
shintamainjp 0:e91b984e285d 209 }
shintamainjp 0:e91b984e285d 210 }
shintamainjp 0:e91b984e285d 211 p++;
shintamainjp 0:e91b984e285d 212 }
shintamainjp 0:e91b984e285d 213 for (cnt = ofs; (nptr[cnt] >= '0') && (nptr[cnt] <= '9'); cnt++) {
shintamainjp 0:e91b984e285d 214 num = 10 * num + (nptr[cnt] - '0');
shintamainjp 0:e91b984e285d 215 }
shintamainjp 0:e91b984e285d 216 if (sign) {
shintamainjp 0:e91b984e285d 217 return -num;
shintamainjp 0:e91b984e285d 218 } else {
shintamainjp 0:e91b984e285d 219 return num;
shintamainjp 0:e91b984e285d 220 }
shintamainjp 0:e91b984e285d 221 }
shintamainjp 0:e91b984e285d 222
shintamainjp 0:e91b984e285d 223 char *ntlibc_strchr(const char *s, int c)
shintamainjp 0:e91b984e285d 224 {
shintamainjp 0:e91b984e285d 225 char *p = (char *)s;
shintamainjp 0:e91b984e285d 226 while (*p) {
shintamainjp 0:e91b984e285d 227 if (*p == c) {
shintamainjp 0:e91b984e285d 228 return p;
shintamainjp 0:e91b984e285d 229 }
shintamainjp 0:e91b984e285d 230 p++;
shintamainjp 0:e91b984e285d 231 }
shintamainjp 0:e91b984e285d 232 return 0;
shintamainjp 0:e91b984e285d 233 }
shintamainjp 0:e91b984e285d 234
shintamainjp 0:e91b984e285d 235 char *ntlibc_utoa(unsigned int value, char *s, int radix)
shintamainjp 0:e91b984e285d 236 {
shintamainjp 0:e91b984e285d 237 char *s1 = s;
shintamainjp 0:e91b984e285d 238 char *s2 = s;
shintamainjp 0:e91b984e285d 239
shintamainjp 0:e91b984e285d 240 do {
shintamainjp 0:e91b984e285d 241 *s2++ = "0123456789abcdefghijklmnopqrstuvwxyz"[value % radix];
shintamainjp 0:e91b984e285d 242 value /= radix;
shintamainjp 0:e91b984e285d 243 } while (value > 0);
shintamainjp 0:e91b984e285d 244
shintamainjp 0:e91b984e285d 245 *s2-- = '\0';
shintamainjp 0:e91b984e285d 246
shintamainjp 0:e91b984e285d 247 while (s1 < s2) {
shintamainjp 0:e91b984e285d 248 char c = *s1;
shintamainjp 0:e91b984e285d 249 *s1++ = *s2;
shintamainjp 0:e91b984e285d 250 *s2-- = c;
shintamainjp 0:e91b984e285d 251 }
shintamainjp 0:e91b984e285d 252
shintamainjp 0:e91b984e285d 253 return s;
shintamainjp 0:e91b984e285d 254 }
shintamainjp 0:e91b984e285d 255
shintamainjp 0:e91b984e285d 256 char *ntlibc_itoa(int value, char *s, int radix)
shintamainjp 0:e91b984e285d 257 {
shintamainjp 0:e91b984e285d 258 char *ptr = s, *ptr1 = s, tmp_char;
shintamainjp 0:e91b984e285d 259 int tmp_value;
shintamainjp 0:e91b984e285d 260
shintamainjp 0:e91b984e285d 261 if ((radix < 2) || (radix > 36)) {
shintamainjp 0:e91b984e285d 262 *s = '\0';
shintamainjp 0:e91b984e285d 263 return s;
shintamainjp 0:e91b984e285d 264 }
shintamainjp 0:e91b984e285d 265 do {
shintamainjp 0:e91b984e285d 266 tmp_value = value;
shintamainjp 0:e91b984e285d 267 value /= radix;
shintamainjp 0:e91b984e285d 268 *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * radix)];
shintamainjp 0:e91b984e285d 269 } while (value);
shintamainjp 0:e91b984e285d 270
shintamainjp 0:e91b984e285d 271 if (tmp_value < 0) {
shintamainjp 0:e91b984e285d 272 *ptr++ = '-';
shintamainjp 0:e91b984e285d 273 }
shintamainjp 0:e91b984e285d 274 *ptr-- = '\0';
shintamainjp 0:e91b984e285d 275 while (ptr1 < ptr) {
shintamainjp 0:e91b984e285d 276 tmp_char = *ptr;
shintamainjp 0:e91b984e285d 277 *ptr--= *ptr1;
shintamainjp 0:e91b984e285d 278 *ptr1++ = tmp_char;
shintamainjp 0:e91b984e285d 279 }
shintamainjp 0:e91b984e285d 280 return s;
shintamainjp 0:e91b984e285d 281 }
shintamainjp 0:e91b984e285d 282
shintamainjp 0:e91b984e285d 283