initial command shell implementation

almost all commands are not yet implemented.

actual communication hooks are not yet implemented.
This commit is contained in:
true 2024-11-06 22:03:59 -08:00
parent 2bee391469
commit d6e68ce761
26 changed files with 6250 additions and 2 deletions

View File

@ -0,0 +1,33 @@
/*
* cons_about.h
*
* Created on: Nov 6, 2024
* Author: true
*/
#ifndef USER_SHELL_COMMANDS_ABOUT_H_
#define USER_SHELL_COMMANDS_ABOUT_H_
static const char about[] = "true's GAT Stand\n"
"a device for displaying badge addons\n"
"compatible with GAT, SAOv1, SAOv1.69bis standards\n\n"
"code and hardware design by true\n"
"manual and info at https://basic.truecontrol.org\n\n";
void cons_about(int argc, char **argv)
{
if (!argc) {
CONS_PRINT("...just do it.");
return;
}
CONS_PRINT(about);
}
#endif /* USER_SHELL_COMMANDS_ABOUT_H_ */

View File

@ -0,0 +1,25 @@
/*
* cons_cls.h
*
* Created on: Nov 6, 2024
* Author: true
*/
#ifndef USER_SHELL_COMMANDS_CLS_H_
#define USER_SHELL_COMMANDS_CLS_H_
void cons_cls(int argc, char **argv)
{
if (!argc) {
CONS_PRINT("...clears the screen. just try it.");
return;
}
CONS_PRINT("\x1B[2J\x1B[0;0H");
}
#endif /* USER_SHELL_COMMANDS_CLS_H_ */

View File

@ -0,0 +1,65 @@
/*
* cons_help.h
*
* Created on: Nov 6, 2024
* Author: true
*/
#ifndef USER_SHELL_COMMANDS_HELP_H_
#define USER_SHELL_COMMANDS_HELP_H_
void cons_help_real(int argc, char **argv, command_table_t *p)
{
int i;
if (argc < 2) {
// print help list
if ((shell_flags & 0x02) == 0) {
CONS_PRINT("Tip: You only need to use the first two characters ");
CONS_PRINT("of a command.\r\n\r\n");
shell_flags |= 0x02;
}
while (p->cmd != 0) {
if (strlen(p->desc)) {
strcpy( console_line, "* ");
strncat(console_line, p->cmd, 12);
for (i = strlen(console_line); i <= 12; i++) {
console_line[i] = 0x20;
}
console_line[i] = 0x00;
strncat(console_line, p->desc, 77);
strncat(console_line, "\r\n", 79);
console_line[63] = 0;
CONS_PRINT(console_line);
}
p++;
}
} else {
while (p->cmd != 0) {
// calling the callback with argc/argv set to 0 should instruct it to print help/usage
if (strcmp(argv[1], p->cmd) == 0) {
p->go(0, (char **)0);
return;
}
p++;
}
// looks like the command wasn't found
strcpy( console_line, "* no help found for command called '");
strncat(console_line, argv[1], 60);
strncat(console_line, "'\r\n", 62);
CONS_PRINT(console_line);
}
}
void cons_help(int argc, char **argv)
{
cons_help_real(argc, argv, (command_table_t *)console_cmdlist);
}
#endif /* USER_SHELL_COMMANDS_HELP_H_ */

View File

@ -0,0 +1,271 @@
/*
* console.c
*
* Created on: Nov 6, 2024
* Author: true
*/
#include "console.h"
#include <stdint.h>
#include <stdio.h>
#include "version.h"
// console
ntshell_t ntshell;
text_history_t ntshell_history;
char ntshell_prompt_old[32];
static uint16_t shell_flags;
static int (*console_puts)(char *buf, int cnt);
static int (*console_gets)(char *buf, int cnt);
// console commands
#include "commands/about.h"
#include "commands/cls.h"
void cons_help(int argc, char **argv);
// console command table
static const command_table_t console_cmdlist[] = {
{"-", 0, "General Commands", 0x00, 0},
{"help", "<cmd-name>", "prints this text. for command help, try help <cmd-name>", 0x00, cons_help},
{"?", 0, 0, 0x00, cons_help},
{"save", 0, "commits data to nvram", 0x00, 0},
{"clear", 0, "clears the screen", 0x00, cons_cls},
{"ver", 0, "prints firmware version number", 0x00, 0},
{"about", 0, "prints info about this product", 0x00, cons_about},
{"-", 0, "I2C Commands", 0x00, 0},
{"scan", 0, "scans i2c bus for responding slaves", 0x00, 0},
{"read", "<addr> <len>", "reads from I2C slave", 0x00, 0},
{"write", "<addr> <len> <hexdata>", "writes to I2C slave", 0x00, 0},
{"-", 0, "Clock Commands", 0x00, 0},
{"time", "[hhmmss]", "prints current time. if time is provided, sets the time.", 0x00, 0},
{"auto", "[on/off", "enables or disables all automatic schedules.", 0x00, 0},
{"calendar", "[id] [on/off] [hhmmss]", "prints an auto schedule. if values set, sets a schedule.", 0x00, 0},
{"-", 0, "Light Level Commands", 0x00, 0},
{"light", 0, "prints the detected light level", 0x00, 0},
{"day", "[new-day-thresh]", "prints bright (on) threshold. if value set, sets threshold.", 0x00, 0},
{"night", "[new-day-thresh]", "prints dark (off) threshold. if value set, sets threshold.", 0x00, 0},
};
command_table_t *ctbl;
command_table_t *ctbl_prev;
// help needs to reference the command table
#include "commands/help.h"
// additional
char console_line[256];
// hijack serial for custom text parsing
// todo: is this required anymore? wtf does it do?
ntshell_t hijack;
// ntshell handlers
static int console_read(char *buf, int cnt, void *extobj)
{
if (console_gets != 0)
return console_gets(buf, cnt);
return 0;
}
int console_write(const char *buf, int cnt, void *extobj)
{
if (console_puts != 0) {
console_puts((char *)buf, cnt);
return cnt;
}
return 0;
}
static int console_ntopt_cb(int argc, char **argv, void *extobj)
{
// ntshell_t *ntshell = (ntshell_t *)extobj;
char cmd[2][4];
command_table_t *p;
if (argc == 0) {
return 0;
}
cmd[0][2] = cmd[1][2] = 0;
strncpy(cmd[0], argv[0], 2);
for (p = ctbl; p->cmd != NULL; p++) {
strncpy(cmd[1], p->cmd, 2);
if ((strcmp(cmd[0], cmd[1]) == 0) && (strlen(argv[0]) <= strlen(p->cmd))) {
p->go(argc, argv);
return 0;
}
}
sprintf(console_line, "* '%s': ", argv[0]);
CONS_PRINT(console_line);
CONS_PRINT("command not found\r\n");
return -1;
}
static int console_cb(const char *text, void *extobj)
{
// ntshell_t *ntshell = (ntshell_t *)extobj;
if ((shell_flags & 0x01) == 0) {
sprintf(console_line, "trueControl GAT Stand, version %s\r\n", FW_VERSION);
CONS_PRINT(console_line);
CONS_PRINT("Use 'help' or '?' for the command list.\r\n");
shell_flags |= 0x01;
return 0;
}
if (strlen(text) > 0) {
return ntopt_parse(text, console_ntopt_cb, extobj);
}
return 0;
}
// setup
void console_init()
{
// set up command table pointer
ctbl = (command_table_t *)console_cmdlist;
// set up the shell
ntshell_init(
&ntshell,
console_read,
console_write,
console_cb,
&ntshell_history,
(void *)&hijack);
ntshell_set_prompt(&ntshell, CONS_PROMPT_DEFAULT);
// ntshell_execute(&ntshell);
// set up the hijacker
ntshell_init(
&hijack,
console_read,
console_write,
NULL,
0,
(void *)&ntshell);
ntshell_set_prompt(&hijack, "");
}
void console_set_puts(int (*puts)(char *buf, int cnt))
{
console_puts = puts;
}
void console_set_gets(int (*gets)(char *buf, int cnt))
{
console_gets = gets;
}
void console_start()
{
ntshell_execute_start(&ntshell);
}
void console_stop()
{
shell_flags = 0;
}
void console_process()
{
ntshell_execute_process(hijack.func_callback ? &hijack : &ntshell);
}
void console_hijack(int (*cb)(const char *text, void *extobj))
{
if (cb) {
// we are in the middle of processing our old shell, and we don't
// want to show the prompt. so back it up and unset it
// before enabling the hijack
strncpy(ntshell_prompt_old, ntshell.prompt, 15);
strcpy(ntshell.prompt, "");
hijack.func_callback = cb;
return;
}
// we're done, so recover our shell prompt and print it
hijack.func_callback = 0;
strncpy(ntshell.prompt, ntshell_prompt_old, 15);
CONS_PRINT("\r\n");
CONS_PRINT(ntshell.prompt);
}
/*
void console_cmd_credits(int argc, char **argv)
{
if (!argc) {
CONS_PRINT("Just run the command.");
return;
}
console_cmd_clear_screen(1, 0);
CONS_PRINT(" It seems you have a badge with our name on it. We are the");
CONS_PRINT("\x1B[3;20H\x1B[01mWHISKEY PIRATE CREW\x1b[00m");
CONS_PRINT("\x1B[5;18HBooze. Badges. Bullshit.\r\n");
}
void console_cmd_clear_screen(int argc, char **argv)
{
if (!argc) {
CONS_PRINT("...");
return;
}
CONS_PRINT("\x1B[2J\x1B[0;0H");
}
void console_settings_save()
{
CONS_PRINT("Saving settings...");
CONS_PRINT("done.\r\n");
}
void console_cmd_save(int argc, char **argv)
{
if (!argc) {
CONS_PRINT("Saves settings to flash. Run the 'save' command for arguments.");
}
if (argc == 1) {
CONS_PRINT("Usage: save <all/setup or settings/alarm/led)\r\n");
}
if (argc == 2) {
strtolower((unsigned char *)argv[1]);
if (strcmp(argv[1], "all") == 0) {
console_settings_save();
console_time_alarm_save();
}
if (strcmp(argv[1], "alarm") == 0) {
console_time_alarm_save();
}
if (strcmp(argv[1], "led") == 0) {
}
if (argv[1][0] == 's' && argv[1][1] == 'e') {
console_settings_save();
}
}
}
*/

View File

@ -0,0 +1,64 @@
/*
* console.h
*/
#ifndef USER_SHELL_CONSOLE_H_
#define USER_SHELL_CONSOLE_H_
#include <stdint.h>
#include <string.h>
// pirate tools
#include "ntshell/core/ntshell.h"
#include "ntshell/util/ntstdio.h"
#include "ntshell/util/ntopt.h"
#define CONS_PROMPT_DEFAULT "\r\ngat> "
#define CONS_PRINT(s) console_write(s, strlen(s), 0)
/* struct */
typedef struct {
char *cmd;
char *usage;
char *desc;
uint8_t flags;
void (*go)(int argc, char **argv);
void (*help)(void);
} command_table_t;
/* extern variables */
extern ntshell_t ntshell;
extern ntshell_t hijack;
extern command_table_t *ctbl;
extern command_table_t *ctbl_prev;
extern char console_line[256];
/* console prototypes */
void console_init();
void console_set_puts(int (*puts)(char *buf, int cnt));
void console_set_gets(int (*gets)(char *buf, int cnt));
void console_start();
void console_stop();
void console_process();
void console_wait();
int console_write(const char *buf, int cnt, void *extobj);
/* hijack the console for per-line input handling */
void console_hijack(int (*cb)(const char *text, void *extobj));
#endif /* USER_SHELL_CONSOLE_H_ */

View File

@ -0,0 +1,52 @@
/**
* @file ntconf.h
* @author CuBeatSystems
* @author Shinichiro Nakamura
* @copyright
* ===============================================================
* Natural Tiny Shell (NT-Shell) Version 0.3.1
* ===============================================================
* Copyright (c) 2010-2016 Shinichiro Nakamura
*
* 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 NTCONF_H
#define NTCONF_H
/**
* @note
* This file provides internal definitions for inner modules.
*/
/**
* @brief Maximum length for the editor module.
*/
#define NTCONF_EDITOR_MAXLEN (250)
/**
* @brief Maximum depth for the history module.
*/
#define NTCONF_HISTORY_DEPTH (20)
#endif

View File

@ -0,0 +1,55 @@
/**
* @file ntint.h
* @author CuBeatSystems
* @author Shinichiro Nakamura
* @copyright
* ===============================================================
* Natural Tiny Shell (NT-Shell) Version 0.3.1
* ===============================================================
* Copyright (c) 2010-2016 Shinichiro Nakamura
*
* 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 NTINT_H
#define NTINT_H
/**
* @note
* This file provides integer definitions.
* You can use #include <stdint.h> if your tool chain have the header.
*/
#if 1
# include <stdint.h>
#else
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef char int8_t;
typedef short int16_t;
typedef int int32_t;
typedef unsigned int size_t;
#endif
#endif

View File

@ -0,0 +1,254 @@
/**
* @file ntlibc.c
* @author CuBeatSystems
* @author Shinichiro Nakamura
* @copyright
* ===============================================================
* Natural Tiny Shell (NT-Shell) Version 0.3.1
* ===============================================================
* Copyright (c) 2010-2016 Shinichiro Nakamura
*
* 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 "ntlibc.h"
int ntlibc_strlen(const char *s)
{
const char *p = s;
int cnt = 0;
while (*p) {
cnt++;
p++;
}
return cnt;
}
char *ntlibc_strcpy(char *des, const char *src)
{
char *d = des;
const char *s = src;
while (*s) {
*d = *s;
d++;
s++;
}
*d = '\0';
return des;
}
char *ntlibc_strcat(char *des, const char *src)
{
char *d = des;
const char *s = src;
while (*d) {
d++;
}
while (*s) {
*d = *s;
d++;
s++;
}
*d = '\0';
return des;
}
int ntlibc_strcmp(const char *s1, const char *s2)
{
char *p1 = (char *)s1;
char *p2 = (char *)s2;
while (*p1 || *p2) {
if (*p1 != *p2) {
return (*p1 < *p2) ? -1 : 1;
}
p1++;
p2++;
}
if (*p1 == *p2) {
return 0;
} else {
return (*p1 < *p2) ? -1 : 1;
}
}
int ntlibc_stricmp(const char *s1, const char *s2)
{
char *p1 = (char *)s1;
char *p2 = (char *)s2;
while (*p1 || *p2) {
if (ntlibc_toupper(*p1) != ntlibc_toupper(*p2)) {
return (*p1 < *p2) ? -1 : 1;
}
p1++;
p2++;
}
if (*p1 == *p2) {
return 0;
} else {
return (*p1 < *p2) ? -1 : 1;
}
}
int ntlibc_strncmp(const char *s1, const char *s2, int n)
{
char *p1 = (char *)s1;
char *p2 = (char *)s2;
int len = 0;
while (*p1 || *p2) {
if (n <= len) {
break;
}
if (*p1 != *p2) {
return (*p1 < *p2) ? -1 : 1;
}
p1++;
p2++;
len++;
}
return 0;
}
int ntlibc_isdigit(int c)
{
if (('0' <= c) && (c <= '9')) {
return 1;
}
return 0;
}
int ntlibc_isalpha(int c)
{
if (('A' <= c) && (c <= 'Z')) {
return 1;
}
if (('a' <= c) && (c <= 'z')) {
return 1;
}
return 0;
}
int ntlibc_iscntrl(int c)
{
if (c == 0x07) { return 0; }
if (c == 0x08) { return 0; }
if (c == 0x09) { return 0; }
if (c == 0x0a) { return 0; }
if (c == 0x0b) { return 0; }
if (c == 0x0c) { return 0; }
if (c == 0x0d) { return 0; }
if ((0x00 <= c) && (c <= 0x1f)) {
return 1;
}
return 0;
}
int ntlibc_toupper(int c)
{
if (('a' <= c) && (c <= 'z')) {
int diff = 'a' - 'A';
return c - diff;
}
return c;
}
int ntlibc_tolower(int c)
{
if (('A' <= c) && (c <= 'Z')) {
int diff = 'a' - 'A';
return c + diff;
}
return c;
}
int ntlibc_atoi(const char *nptr)
{
int cnt;
int num = 0;
int ofs = 0;
int sign = 0;
int scnt = 0;
char *p = (char *)nptr;
while (*p != '\0') {
if (!ntlibc_isdigit(*p)) {
if (*p == ' ') {
ofs++;
}
if (*p == '+') {
sign = 0;
ofs++;
if (scnt++ > 0) {
return 0;
}
}
if (*p == '-') {
sign = 1;
ofs++;
if (scnt++ > 0) {
return 0;
}
}
}
p++;
}
for (cnt = ofs; (nptr[cnt] >= '0') && (nptr[cnt] <= '9'); cnt++) {
num = 10 * num + (nptr[cnt] - '0');
}
if (sign) {
return -num;
} else {
return num;
}
}
char *ntlibc_strchr(const char *s, int c)
{
char *p = (char *)s;
while (*p) {
if (*p == c) {
return p;
}
p++;
}
return 0;
}
char *ntlibc_utoa(unsigned int value, char *s, int radix)
{
char *s1 = s;
char *s2 = s;
do {
*s2++ = "0123456789abcdefghijklmnopqrstuvwxyz"[value % radix];
value /= radix;
} while (value > 0);
*s2-- = '\0';
while (s1 < s2) {
char c = *s1;
*s1++ = *s2;
*s2-- = c;
}
return s;
}

View File

@ -0,0 +1,60 @@
/**
* @file ntlibc.h
* @author CuBeatSystems
* @author Shinichiro Nakamura
* @copyright
* ===============================================================
* Natural Tiny Shell (NT-Shell) Version 0.3.1
* ===============================================================
* Copyright (c) 2010-2016 Shinichiro Nakamura
*
* 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 NTLIBC_H
#define NTLIBC_H
#ifdef __cplusplus
extern "C" {
#endif
int ntlibc_strlen(const char *s);
char *ntlibc_strcpy(char *des, const char *src);
char *ntlibc_strcat(char *des, const char *src);
int ntlibc_strcmp(const char *s1, const char *s2);
int ntlibc_stricmp(const char *s1, const char *s2);
int ntlibc_strncmp(const char *s1, const char *s2, int n);
int ntlibc_isdigit(int c);
int ntlibc_isalpha(int c);
int ntlibc_iscntrl(int c);
int ntlibc_toupper(int c);
int ntlibc_tolower(int c);
int ntlibc_atoi(const char *nptr);
char *ntlibc_strchr(const char *s, int c);
char *ntlibc_utoa(unsigned int value, char *s, int radix);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,683 @@
/**
* @file ntshell.c
* @author CuBeatSystems
* @author Shinichiro Nakamura
* @copyright
* ===============================================================
* Natural Tiny Shell (NT-Shell) Version 0.3.1
* ===============================================================
* Copyright (c) 2010-2016 Shinichiro Nakamura
*
* 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 "ntshell.h"
#include "ntlibc.h"
#define VERSION_MAJOR (0) /**< Major number. */
#define VERSION_MINOR (2) /**< Minor number. */
#define VERSION_RELEASE (0) /**< Release number. */
/**
* @brief Initialization code.
*/
#define INITCODE (0x4367)
/**
* @brief Unused variable wrapper.
*
* @param N A variable.
*/
#define UNUSED_VARIABLE(N) do { (void)(N); } while (0)
/**
* @brief Index number of the suggestion.
*
* @param HANDLE A pointer of the handle.
*/
#define SUGGEST_INDEX(HANDLE) ((HANDLE)->suggest_index)
/**
* @brief Source text string of the suggestion.
*
* @param HANDLE A pointer of the handle.
*/
#define SUGGEST_SOURCE(HANDLE) ((HANDLE)->suggest_source)
/**
* @brief Get the text editor.
*
* @param HANDLE A pointer of the handle.
*/
#define GET_EDITOR(HANDLE) (&((HANDLE)->editor))
/**
* @brief Get the text history.
*
* @param HANDLE A pointer of the handle.
*/
#define GET_HISTORY(HANDLE) (((HANDLE)->history))
/**
* @brief Read from the serial port.
*
* @param HANDLE A pointer of the handle.
* @param BUF A pointer to the buffer.
* @param CNT Read length.
*
* @return The number of bytes read.
*/
#define SERIAL_READ(HANDLE, BUF, CNT) ((HANDLE)->func_read(BUF, CNT, (HANDLE)->extobj))
/**
* @brief Write to the serial port.
*
* @param HANDLE A pointer of the handle.
* @param BUF A pointer to the buffer.
* @param CNT Write length.
*
* @return The number of bytes written.
*/
#define SERIAL_WRITE(HANDLE, BUF, CNT) ((HANDLE)->func_write(BUF, CNT, (HANDLE)->extobj))
/**
* @brief Write the prompt to the serial port.
*
* @param HANDLE A pointer of the handle.
*/
#define PROMPT_WRITE(HANDLE) SERIAL_WRITE((HANDLE), (HANDLE)->prompt, ntlibc_strlen((HANDLE)->prompt))
/**
* @brief Write the newline to the serial port.
*
* @param HANDLE A pointer of the handle.
*/
#define PROMPT_NEWLINE(HANDLE) SERIAL_WRITE((HANDLE), NTSHELL_PROMPT_NEWLINE, ntlibc_strlen(NTSHELL_PROMPT_NEWLINE))
/**
* @brief Call the user callback function.
*
* @param HANDLE A pointer of the handle.
* @param TEXT A text string for the callback function.
*/
#define CALLBACK(HANDLE, TEXT) ((HANDLE)->func_callback((TEXT), (HANDLE)->extobj))
#define VTSEND_ERASE_LINE(HANDLE) (vtsend_erase_line(&((HANDLE)->vtsend)))
#define VTSEND_CURSOR_HEAD(HANDLE) (vtsend_cursor_backward(&((HANDLE)->vtsend), 80))
#define VTSEND_CURSOR_PREV(HANDLE) (vtsend_cursor_backward(&((HANDLE)->vtsend), 1))
#define VTSEND_CURSOR_NEXT(HANDLE) (vtsend_cursor_forward(&((HANDLE)->vtsend), 1))
/**
* @brief Search a previous input text on the history module.
* @details This function change the state of the logical text editor and the view.
*
* @param ntshell A handler of the NT-Shell.
* @param action An action.
* @param ch A input character.
*/
static void actfunc_history_prev(ntshell_t *ntshell, vtrecv_action_t action, unsigned char ch)
{
UNUSED_VARIABLE(action);
UNUSED_VARIABLE(ch);
if (text_history_read_point_prev(GET_HISTORY(ntshell))) {
char txt[TEXTHISTORY_MAXLEN];
int n = text_history_read(GET_HISTORY(ntshell), &txt[0], sizeof(txt));
if (0 < n) {
VTSEND_ERASE_LINE(ntshell);
VTSEND_CURSOR_HEAD(ntshell);
PROMPT_WRITE(ntshell);
SERIAL_WRITE(ntshell, txt, n);
text_editor_set_text(GET_EDITOR(ntshell), txt);
}
}
}
/**
* @brief Search a next input text on the history module.
* @details This function change the state of the logical text editor and the view.
*
* @param ntshell A handler of the NT-Shell.
* @param action An action.
* @param ch A input character.
*/
static void actfunc_history_next(ntshell_t *ntshell, vtrecv_action_t action, unsigned char ch)
{
UNUSED_VARIABLE(action);
UNUSED_VARIABLE(ch);
if (text_history_read_point_next(GET_HISTORY(ntshell))) {
char txt[TEXTHISTORY_MAXLEN];
int n = text_history_read(GET_HISTORY(ntshell), &txt[0], sizeof(txt));
if (0 < n) {
VTSEND_ERASE_LINE(ntshell);
VTSEND_CURSOR_HEAD(ntshell);
PROMPT_WRITE(ntshell);
SERIAL_WRITE(ntshell, txt, n);
text_editor_set_text(GET_EDITOR(ntshell), txt);
}
}
}
/**
* @brief Move the cursor to left.
* @details This function change the state of the logical text editor and the view.
*
* @param ntshell A handler of the NT-Shell.
* @param action An action.
* @param ch A input character.
*/
static void actfunc_cursor_left(ntshell_t *ntshell, vtrecv_action_t action, unsigned char ch)
{
UNUSED_VARIABLE(action);
UNUSED_VARIABLE(ch);
if (text_editor_cursor_left(GET_EDITOR(ntshell))) {
VTSEND_CURSOR_PREV(ntshell);
}
}
/**
* @brief Move the cursor to right.
* @details This function change the state of the logical text editor and the view.
*
* @param ntshell A handler of the NT-Shell.
* @param action An action.
* @param ch A input character.
*/
static void actfunc_cursor_right(ntshell_t *ntshell, vtrecv_action_t action, unsigned char ch)
{
UNUSED_VARIABLE(action);
UNUSED_VARIABLE(ch);
if (text_editor_cursor_right(GET_EDITOR(ntshell))) {
VTSEND_CURSOR_NEXT(ntshell);
}
}
/**
* @brief Process for the enter action.
* @details This function change the state of the logical text editor and the view.
*
* @param ntshell A handler of the NT-Shell.
* @param action An action.
* @param ch A input character.
*/
static void actfunc_enter(ntshell_t *ntshell, vtrecv_action_t action, unsigned char ch)
{
char txt[TEXTEDITOR_MAXLEN];
UNUSED_VARIABLE(action);
UNUSED_VARIABLE(ch);
text_editor_get_text(GET_EDITOR(ntshell), &txt[0], sizeof(txt));
text_editor_clear(GET_EDITOR(ntshell));
text_history_write(GET_HISTORY(ntshell), txt);
PROMPT_NEWLINE(ntshell);
CALLBACK(ntshell, txt);
PROMPT_WRITE(ntshell);
}
/**
* @brief Process for the cancel action.
* @details This function change the state of the logical text editor and the view.
*
* @note
* The CTRL+C operation in the general OS uses a signal.
* In this cancel action, It simulate only the view.
*
* @param ntshell A handler of the NT-Shell.
* @param action An action.
* @param ch A input character.
*/
static void actfunc_cancel(ntshell_t *ntshell, vtrecv_action_t action, unsigned char ch)
{
UNUSED_VARIABLE(action);
UNUSED_VARIABLE(ch);
text_editor_clear(GET_EDITOR(ntshell));
SERIAL_WRITE(ntshell, "^C", 2);
PROMPT_NEWLINE(ntshell);
PROMPT_WRITE(ntshell);
}
/**
* @brief Process for the insert action.
* @details This function change the state of the logical text editor and the view.
*
* @param ntshell A handler of the NT-Shell.
* @param action An action.
* @param ch A input character.
*/
static void actfunc_insert(ntshell_t *ntshell, vtrecv_action_t action, unsigned char ch)
{
UNUSED_VARIABLE(action);
/*
* Reject the suggestion index number if an input action occurred.
*/
SUGGEST_INDEX(ntshell) = -1;
/*
* Insert the input character using the logical text editor.
* Update the view.
*/
if (text_editor_insert(GET_EDITOR(ntshell), ch)) {
char txt[TEXTEDITOR_MAXLEN];
int len = text_editor_get_text(GET_EDITOR(ntshell), &txt[0], sizeof(txt));
int pos = text_editor_cursor_get_position(GET_EDITOR(ntshell));
int n = len - pos;
SERIAL_WRITE(ntshell, (char *)&ch, sizeof(ch));
if (n > 0) {
int i;
SERIAL_WRITE(ntshell, txt + pos, len - pos);
for (i = 0; i < n; i++) {
VTSEND_CURSOR_PREV(ntshell);
}
}
}
}
/**
* @brief Process for the backspace action.
* @details This function change the state of the logical text editor and the view.
*
* @param ntshell A handler of the NT-Shell.
* @param action An action.
* @param ch A input character.
*/
static void actfunc_backspace(ntshell_t *ntshell, vtrecv_action_t action, unsigned char ch)
{
UNUSED_VARIABLE(action);
UNUSED_VARIABLE(ch);
if (text_editor_backspace(GET_EDITOR(ntshell))) {
char txt[TEXTEDITOR_MAXLEN];
int len = text_editor_get_text(GET_EDITOR(ntshell), &txt[0], sizeof(txt));
int pos = text_editor_cursor_get_position(GET_EDITOR(ntshell));
int n = len - pos;
VTSEND_CURSOR_PREV(ntshell);
if (n > 0) {
int i;
SERIAL_WRITE(ntshell, txt + pos, n);
SERIAL_WRITE(ntshell, " ", 1);
for (i = 0; i < n + 1; i++) {
VTSEND_CURSOR_PREV(ntshell);
}
} else {
SERIAL_WRITE(ntshell, " ", 1);
VTSEND_CURSOR_PREV(ntshell);
}
}
}
/**
* @brief Process for the delete action.
* @details This function change the state of the logical text editor and the view.
*
* @param ntshell A handler of the NT-Shell.
* @param action An action.
* @param ch A input character.
*/
static void actfunc_delete(ntshell_t *ntshell, vtrecv_action_t action, unsigned char ch)
{
UNUSED_VARIABLE(action);
UNUSED_VARIABLE(ch);
if (text_editor_delete(GET_EDITOR(ntshell))) {
char txt[TEXTEDITOR_MAXLEN];
int len = text_editor_get_text(GET_EDITOR(ntshell), &txt[0], sizeof(txt));
int pos = text_editor_cursor_get_position(GET_EDITOR(ntshell));
int n = len - pos;
if (n > 0) {
int i;
SERIAL_WRITE(ntshell, txt + pos, n);
SERIAL_WRITE(ntshell, " ", 1);
for (i = 0; i < n + 1; i++) {
VTSEND_CURSOR_PREV(ntshell);
}
} else {
SERIAL_WRITE(ntshell, " ", 1);
VTSEND_CURSOR_PREV(ntshell);
}
}
}
/**
* @brief Process for the suggestion action.
* @details This function change the state of the logical text editor and the view.
*
* @param ntshell A handler of the NT-Shell.
* @param action An action.
* @param ch A input character.
*/
static void actfunc_suggest(ntshell_t *ntshell, vtrecv_action_t action, unsigned char ch)
{
char buf[TEXTEDITOR_MAXLEN];
UNUSED_VARIABLE(action);
UNUSED_VARIABLE(ch);
if (SUGGEST_INDEX(ntshell) < 0) {
/*
* Enter the input suggestion mode.
* Get the suggested text string with the current text string.
*/
if (text_editor_get_text(
GET_EDITOR(ntshell),
SUGGEST_SOURCE(ntshell),
sizeof(SUGGEST_SOURCE(ntshell))) > 0) {
SUGGEST_INDEX(ntshell) = 0;
if (text_history_find(
GET_HISTORY(ntshell),
SUGGEST_INDEX(ntshell),
SUGGEST_SOURCE(ntshell),
buf,
sizeof(buf)) == 0) {
/*
* Found the suggestion.
*/
int n = ntlibc_strlen((const char *)buf);
VTSEND_ERASE_LINE(ntshell);
VTSEND_CURSOR_HEAD(ntshell);
PROMPT_WRITE(ntshell);
SERIAL_WRITE(ntshell, buf, n);
text_editor_set_text(GET_EDITOR(ntshell), buf);
} else {
/*
* Not found the suggestion.
*/
SUGGEST_INDEX(ntshell) = -1;
}
}
} else {
/*
* Already the input suggestion mode.
* Search the next suggestion text string.
*/
SUGGEST_INDEX(ntshell) = SUGGEST_INDEX(ntshell) + 1;
if (text_history_find(
GET_HISTORY(ntshell),
SUGGEST_INDEX(ntshell),
SUGGEST_SOURCE(ntshell),
buf,
sizeof(buf)) == 0) {
/*
* Found the suggestion.
*/
int n = ntlibc_strlen((const char *)buf);
VTSEND_ERASE_LINE(ntshell);
VTSEND_CURSOR_HEAD(ntshell);
PROMPT_WRITE(ntshell);
SERIAL_WRITE(ntshell, buf, n);
text_editor_set_text(GET_EDITOR(ntshell), buf);
} else {
/*
* Not found the suggestion.
* Recall the previous input text string.
*/
int n = ntlibc_strlen(SUGGEST_SOURCE(ntshell));
VTSEND_ERASE_LINE(ntshell);
VTSEND_CURSOR_HEAD(ntshell);
PROMPT_WRITE(ntshell);
SERIAL_WRITE(ntshell, SUGGEST_SOURCE(ntshell), n);
text_editor_set_text(GET_EDITOR(ntshell), SUGGEST_SOURCE(ntshell));
SUGGEST_INDEX(ntshell) = -1;
}
}
}
/**
* @brief Move the cursor to the head of the line.
* @details This function change the state of the logical text editor and the view.
*
* @param ntshell A handler of the NT-Shell.
* @param action An action.
* @param ch A input character.
*/
static void actfunc_cursor_head(ntshell_t *ntshell, vtrecv_action_t action, unsigned char ch)
{
UNUSED_VARIABLE(action);
UNUSED_VARIABLE(ch);
VTSEND_CURSOR_HEAD(ntshell);
PROMPT_WRITE(ntshell);
text_editor_cursor_head(GET_EDITOR(ntshell));
}
/**
* @brief Move the cursor to the tail of the line.
* @details This function change the state of the logical text editor and the view.
*
* @param ntshell A handler of the NT-Shell.
* @param action An action.
* @param ch A input character.
*/
static void actfunc_cursor_tail(ntshell_t *ntshell, vtrecv_action_t action, unsigned char ch)
{
char buf[TEXTEDITOR_MAXLEN];
int len;
UNUSED_VARIABLE(action);
UNUSED_VARIABLE(ch);
text_editor_get_text(GET_EDITOR(ntshell), buf, sizeof(buf));
len = ntlibc_strlen((const char *)buf);
VTSEND_CURSOR_HEAD(ntshell);
PROMPT_WRITE(ntshell);
SERIAL_WRITE(ntshell, buf, len);
text_editor_cursor_tail(GET_EDITOR(ntshell));
}
/**
* @brief The data structure of the action table.
* @details
* The action consists from the state and the input character.
* This definition also have the callback function.
*/
typedef struct {
vtrecv_action_t action;
unsigned char ch;
void (*func)(ntshell_t *ntshell, vtrecv_action_t action, unsigned char ch);
} ntshell_action_table_t;
/**
* @brief Process function table for the actions.
* @details
* The action codes depends on the virtual terminals.
* So you should check some virtual terminal softwares and the environments.
*
* <table>
* <th>
* <td>Platform</td>
* <td>Tools</td>
* </th>
* <tr>
* <td>Windows</td>
* <td>Hyper Terminal, Poderossa, TeraTerm</td>
* </tr>
* <tr>
* <td>Linux</td>
* <td>minicom, screen, kermit</td>
* </tr>
* </table>
*/
static const ntshell_action_table_t action_table[] = {
{VTRECV_ACTION_EXECUTE, 0x01, actfunc_cursor_head},
{VTRECV_ACTION_EXECUTE, 0x02, actfunc_cursor_left},
{VTRECV_ACTION_EXECUTE, 0x03, actfunc_cancel},
{VTRECV_ACTION_EXECUTE, 0x04, actfunc_delete},
{VTRECV_ACTION_EXECUTE, 0x05, actfunc_cursor_tail},
{VTRECV_ACTION_EXECUTE, 0x06, actfunc_cursor_right},
{VTRECV_ACTION_EXECUTE, 0x08, actfunc_backspace},
{VTRECV_ACTION_EXECUTE, 0x09, actfunc_suggest},
{VTRECV_ACTION_EXECUTE, 0x0d, actfunc_enter},
{VTRECV_ACTION_EXECUTE, 0x0e, actfunc_history_next},
{VTRECV_ACTION_EXECUTE, 0x10, actfunc_history_prev},
{VTRECV_ACTION_CSI_DISPATCH, 0x41, actfunc_history_prev},
{VTRECV_ACTION_CSI_DISPATCH, 0x42, actfunc_history_next},
{VTRECV_ACTION_CSI_DISPATCH, 0x43, actfunc_cursor_right},
{VTRECV_ACTION_CSI_DISPATCH, 0x44, actfunc_cursor_left},
{VTRECV_ACTION_CSI_DISPATCH, 0x7e, actfunc_delete},
{VTRECV_ACTION_PRINT, 0x7f, actfunc_backspace},
};
/**
* @brief The callback function for the vtrecv module.
*
* @param vtrecv The vtrecv.
* @param action An action.
* @param ch A character.
*/
void vtrecv_callback(vtrecv_t *vtrecv, vtrecv_action_t action, unsigned char ch)
{
ntshell_action_table_t *p;
int i;
const int ACTTBLSIZ = sizeof(action_table) / sizeof(action_table[0]);
/*
* Search the process function for the control codes.
*/
p = (ntshell_action_table_t *)action_table;
for (i = 0; i < ACTTBLSIZ; i++) {
if ((p->action == action) && (p->ch == ch)) {
p->func(vtrecv->user_data, action, ch);
return;
}
p++;
}
/*
* A general character is the input character.
*/
if (VTRECV_ACTION_PRINT == action) {
actfunc_insert(vtrecv->user_data, action, ch);
return;
}
/*
* Other cases, there is no defined process function for the input codes.
* If you need to support the input action, you should update the action table.
*/
}
/**
* @brief Initialize the NT-Shell.
*
* @param p A pointer to the handler of NT-Shell.
* @param func_read Serial read function.
* @param func_write Serial write function.
* @param func_callback Callback function.
* @param history History struct, optional.
* @param extobj An external object for the callback function.
*/
void ntshell_init(ntshell_t *p,
NTSHELL_SERIAL_READ func_read,
NTSHELL_SERIAL_WRITE func_write,
NTSHELL_USER_CALLBACK func_callback,
text_history_t *history,
void *extobj)
{
/*
* The vtrecv module provides a pointer interface to an external object.
* NT-Shell uses the text editor, text history, read function, write function with the pointer interface.
*/
p->func_read = func_read;
p->func_write = func_write;
p->func_callback = func_callback;
p->extobj = extobj;
p->history = history;
ntlibc_strcpy(p->prompt, NTSHELL_PROMPT_DEFAULT);
p->vtrecv.user_data = p;
/*
* Initialize the modules.
*/
vtsend_init(&(p->vtsend), func_write, extobj);
vtrecv_init(&(p->vtrecv), vtrecv_callback);
text_editor_init(GET_EDITOR(p));
text_history_init(GET_HISTORY(p));
SUGGEST_INDEX(p) = -1;
/*
* Set the initialization code.
*/
p->initcode = INITCODE;
}
/**
* @brief Execute the NT-Shell.
* @details Never return from this function.
*
* @param p A pointer to the handler of the NT-Shell.
*/
void ntshell_execute(ntshell_t *p)
{
/*
* Check the initialization code.
*/
if (p->initcode != INITCODE) {
return;
}
/*
* User input loop.
*/
PROMPT_WRITE(p);
while (1) {
unsigned char ch;
SERIAL_READ(p, (char *)&ch, sizeof(ch));
vtrecv_execute(&(p->vtrecv), &ch, sizeof(ch));
}
}
void ntshell_execute_start(ntshell_t *p)
{
PROMPT_WRITE(p);
}
void ntshell_execute_process(ntshell_t *p)
{
unsigned char ch;
SERIAL_READ(p, (char *)&ch, sizeof(ch));
vtrecv_execute(&(p->vtrecv), &ch, sizeof(ch));
}
/**
* @brief Set up the prompt of the NT-Shell.
*
* @param p A pointer to the handler of the NT-Shell.
* @param prompt A text string.
*/
void ntshell_set_prompt(ntshell_t *p, const char *prompt)
{
/*
* Check the initialization code.
*/
if (p->initcode != INITCODE) {
return;
}
ntlibc_strcpy(p->prompt, prompt);
}
/**
* @brief Get the version.
*
* @param major Major number.
* @param minor Minor number.
* @param release Release number.
*/
void ntshell_version(int *major, int *minor, int *release)
{
*major = VERSION_MAJOR;
*minor = VERSION_MINOR;
*release = VERSION_RELEASE;
}

View File

@ -0,0 +1,94 @@
/**
* @file ntshell.h
* @author CuBeatSystems
* @author Shinichiro Nakamura
* @copyright
* ===============================================================
* Natural Tiny Shell (NT-Shell) Version 0.3.1
* ===============================================================
* Copyright (c) 2010-2016 Shinichiro Nakamura
*
* 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 NTSHELL_H
#define NTSHELL_H
#include "vtrecv.h"
#include "vtsend.h"
#include "text_editor.h"
#include "text_history.h"
#define NTSHELL_PROMPT_MAXLEN (32)
#define NTSHELL_PROMPT_DEFAULT ">"
#define NTSHELL_PROMPT_NEWLINE "\r\n"
typedef int (*NTSHELL_SERIAL_READ)(char *buf, int cnt, void *extobj);
typedef int (*NTSHELL_SERIAL_WRITE)(const char *buf, int cnt, void *extobj);
typedef int (*NTSHELL_USER_CALLBACK)(const char *text, void *extobj);
/**
* @brief The handler of NT-Shell.
* @details
* The best way to provide a handler, We should hide the implementation from the library users.
* But some small embedded environments can not use memory allocation dynamically.
* So this implementation open the fields of the handler in this header.
* You can use this handler on the stacks.
*/
typedef struct {
unsigned int initcode; /**< Initialization flag. */
vtsend_t vtsend; /**< The handler of vtsend. */
vtrecv_t vtrecv; /**< The handler of vtrecv. */
text_editor_t editor; /**< The handler of text_editor. */
text_history_t *history; /**< The handler of text_history. */
int suggest_index;
char suggest_source[TEXTEDITOR_MAXLEN];
NTSHELL_SERIAL_READ func_read;
NTSHELL_SERIAL_WRITE func_write;
NTSHELL_USER_CALLBACK func_callback;
void *extobj;
char prompt[NTSHELL_PROMPT_MAXLEN];
} ntshell_t;
#ifdef __cplusplus
extern "C" {
#endif
void ntshell_init(ntshell_t *p,
NTSHELL_SERIAL_READ func_read,
NTSHELL_SERIAL_WRITE func_write,
NTSHELL_USER_CALLBACK func_callback,
text_history_t *history,
void *extobj);
void ntshell_execute(ntshell_t *p);
void ntshell_set_prompt(ntshell_t *p, const char *prompt);
void ntshell_version(int *major, int *minor, int *release);
void ntshell_execute_start(ntshell_t *p);
void ntshell_execute_process(ntshell_t *p);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,250 @@
/**
* @file text_editor.c
* @author CuBeatSystems
* @author Shinichiro Nakamura
* @copyright
* ===============================================================
* Natural Tiny Shell (NT-Shell) Version 0.3.1
* ===============================================================
* Copyright (c) 2010-2016 Shinichiro Nakamura
*
* 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 "text_editor.h"
/**
* @brief Initialize the text editor module.
*
* @param p A pointer to the text editor handler.
*/
void text_editor_init(text_editor_t *p)
{
p->pos = 0;
p->len = 0;
p->buffer[p->len] = '\0';
}
/**
* @brief Insert a character.
*
* @param p A pointer to the text editor handler.
* @param c A character.
*/
int text_editor_insert(text_editor_t *p, char c)
{
if (p->len < (int)sizeof(p->buffer) - 1) {
int n = p->len - p->pos + 1;
int i;
char *src = p->buffer + p->len + 0;
char *des = p->buffer + p->len + 1;
for (i = 0; i < n; i++) {
*des = *src;
des--;
src--;
}
p->buffer[p->pos] = c;
p->pos++;
p->len++;
p->buffer[p->len] = '\0';
return 1;
}
return 0;
}
/**
* @brief Delete a character.
*
* @param p A pointer to the text editor handler.
*/
int text_editor_backspace(text_editor_t *p)
{
if (0 < p->pos) {
int n = p->len - p->pos;
int i;
char *src = p->buffer + p->pos - 0;
char *des = p->buffer + p->pos - 1;
p->pos--;
p->len--;
for (i = 0; i < n; i++) {
*des = *src;
des++;
src++;
}
*(p->buffer + p->len) = '\0';
return 1;
}
return 0;
}
/**
* @brief Delete a character.
*
* @param p A pointer to the text editor handler.
*/
int text_editor_delete(text_editor_t *p)
{
if (p->pos < p->len) {
int n = p->len - p->pos - 1;
int i;
char *src = p->buffer + p->pos + 1;
char *des = p->buffer + p->pos + 0;
p->len--;
for (i = 0; i < n; i++) {
*des = *src;
des++;
src++;
}
*(p->buffer + p->len) = '\0';
return 1;
}
return 0;
}
/**
* @brief Get the cursor position.
*
* @param p A pointer to the text editor handler.
*/
int text_editor_cursor_get_position(text_editor_t *p)
{
return p->pos;
}
/**
* @brief Move to the cursor to the head of the line.
*
* @param p A pointer to the text editor handler.
*/
int text_editor_cursor_head(text_editor_t *p)
{
if (0 < p->pos) {
p->pos = 0;
return 1;
}
return 0;
}
/**
* @brief Move to the cursor to the tail of the line.
*
* @param p A pointer to the text editor handler.
*/
int text_editor_cursor_tail(text_editor_t *p)
{
if (p->pos < p->len) {
p->pos = p->len;
return 1;
}
return 0;
}
/**
* @brief Move to the cursor to left.
*
* @param p A pointer to the text editor handler.
*/
int text_editor_cursor_left(text_editor_t *p)
{
if (0 < p->pos) {
p->pos--;
return 1;
}
return 0;
}
/**
* @brief Move to the cursor to right.
*
* @param p A pointer to the text editor handler.
*/
int text_editor_cursor_right(text_editor_t *p)
{
if (p->pos < p->len) {
p->pos++;
return 1;
}
return 0;
}
/**
* @brief Set the edit line.
*
* @param p A pointer to the text editor handler.
* @param buf A text string.
*/
int text_editor_set_text(text_editor_t *p, char *buf)
{
char *src = buf;
char *des = p->buffer;
int n = 0;
while (*src) {
*des = *src;
des++;
src++;
n++;
if ((int)sizeof(p->buffer) <= n - 1) {
break;
}
}
*des = '\0';
p->len = n;
p->pos = p->len;
return n;
}
/**
* @brief Get the edit line.
*
* @param p A pointer to the text editor handler.
* @param buf A text string.
* @param siz Size of the text string buffer.
*/
int text_editor_get_text(text_editor_t *p, char *buf, int siz)
{
char *src = p->buffer;
char *des = buf;
int n = 0;
while (*src) {
*des++ = *src++;
n++;
if (siz <= n) {
break;
}
}
*des = '\0';
return n;
}
/**
* @brief Clear the text string.
*
* @param p A pointer to the text editor handler.
*/
void text_editor_clear(text_editor_t *p)
{
p->pos = 0;
p->len = 0;
p->buffer[p->len] = '\0';
}

View File

@ -0,0 +1,74 @@
/**
* @file text_editor.h
* @author CuBeatSystems
* @author Shinichiro Nakamura
* @copyright
* ===============================================================
* Natural Tiny Shell (NT-Shell) Version 0.3.1
* ===============================================================
* Copyright (c) 2010-2016 Shinichiro Nakamura
*
* 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 TEXT_EDITOR_H
#define TEXT_EDITOR_H
#include "ntconf.h"
/**
* @brief Maximum length of the text string.
*/
#define TEXTEDITOR_MAXLEN (NTCONF_EDITOR_MAXLEN)
/**
* @brief Text editor handler.
*/
typedef struct {
char buffer[TEXTEDITOR_MAXLEN]; /**< Buffer for the text string. */
int pos; /**< Position of the logical cursor. */
int len; /**< Length of the text string. */
} text_editor_t;
#ifdef __cplusplus
extern "C" {
#endif
void text_editor_init(text_editor_t *p);
int text_editor_insert(text_editor_t *p, char c);
int text_editor_backspace(text_editor_t *p);
int text_editor_delete(text_editor_t *p);
int text_editor_cursor_get_position(text_editor_t *p);
int text_editor_cursor_head(text_editor_t *p);
int text_editor_cursor_tail(text_editor_t *p);
int text_editor_cursor_left(text_editor_t *p);
int text_editor_cursor_right(text_editor_t *p);
int text_editor_set_text(text_editor_t *p, char *buf);
int text_editor_get_text(text_editor_t *p, char *buf, int siz);
void text_editor_clear(text_editor_t *p);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,170 @@
/**
* @file text_history.c
* @author CuBeatSystems
* @author Shinichiro Nakamura
* @copyright
* ===============================================================
* Natural Tiny Shell (NT-Shell) Version 0.3.1
* ===============================================================
* Copyright (c) 2010-2016 Shinichiro Nakamura
*
* 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 "text_history.h"
#include "ntlibc.h"
/**
* @brief Initialize this module.
*
* @param p A pointer to the handler.
*/
void text_history_init(text_history_t *p)
{
int i;
p->rp = 0;
p->wp = 0;
for (i = 0; i < (int)sizeof(p->history); i++) {
p->history[i] = 0;
}
}
/**
* @brief Write to the history.
*
* @param p A pointer to the handler.
* @param buf A pointer to the buffer.
*/
int text_history_write(text_history_t *p, char *buf)
{
char *sp = p->history + (TEXTHISTORY_MAXLEN * p->wp);
if (buf[0] == '\0') {
return 0;
}
while (*buf) {
*sp = *buf;
sp++;
buf++;
}
*sp = '\0';
p->wp = (p->wp + 1) % TEXTHISTORY_DEPTH;
p->rp = p->wp;
return 1;
}
/**
* @brief Read from the history.
*
* @param p A pointer to the handler.
* @param buf A pointer to the buffer.
* @param siz A size of the buffer.
*/
int text_history_read(text_history_t *p, char *buf, const int siz)
{
char *sp = p->history + (TEXTHISTORY_MAXLEN * p->rp);
int n = 0;
while (*sp) {
*buf = *sp;
buf++;
sp++;
n++;
if (siz - 1 <= n) {
break;
}
}
*buf = '\0';
return n;
}
/**
* @brief Change the pointing location to the next.
*
* @param p A pointer to the handler.
*/
int text_history_read_point_next(text_history_t *p)
{
int n = (p->rp + 1) % TEXTHISTORY_DEPTH;
if (n != p->wp) {
p->rp = n;
return 1;
}
return 0;
}
/**
* @brief Change the pointing location to the previous.
*
* @param p A pointer to the handler.
*/
int text_history_read_point_prev(text_history_t *p)
{
int n = (p->rp == 0) ? (TEXTHISTORY_DEPTH - 1) : (p->rp - 1);
if (n != p->wp) {
char *sp = p->history + (TEXTHISTORY_MAXLEN * n);
if (*sp != '\0') {
p->rp = n;
return 1;
}
}
return 0;
}
/**
* @brief Find the given text string from the text history.
*
* @param p A pointer to the handler.
* @param index An index number of the history.
* @param text The target text string.
* @param buf A pointer to the buffer.
* @param siz A size of the buffer.
*
* @retval 0 Success.
* @retval !0 Failure.
*/
int text_history_find(text_history_t *p,
const int index, const char *text,
char *buf, const int siz)
{
const int text_len = ntlibc_strlen((const char *)text);
int found = 0;
int i;
for (i = 0; i < TEXTHISTORY_DEPTH; i++) {
int target = (p->rp + i) % TEXTHISTORY_DEPTH;
char *txtp = p->history + (TEXTHISTORY_MAXLEN * target);
const int target_len = ntlibc_strlen((const char *)txtp);
int comp_len = (target_len < text_len) ? target_len : text_len;
if ((ntlibc_strncmp(
(const char *)txtp,
(const char *)text, comp_len) == 0) && (comp_len > 0)) {
if (found == index) {
if (siz <= ntlibc_strlen(txtp)) {
return -1;
}
ntlibc_strcpy((char *)buf, (char *)txtp);
return 0;
}
found++;
}
}
return -1;
}

View File

@ -0,0 +1,84 @@
/**
* @file text_history.h
* @author CuBeatSystems
* @author Shinichiro Nakamura
* @copyright
* ===============================================================
* Natural Tiny Shell (NT-Shell) Version 0.3.1
* ===============================================================
* Copyright (c) 2010-2016 Shinichiro Nakamura
*
* 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 TEXT_HISTORY_H
#define TEXT_HISTORY_H
#include "ntconf.h"
/**
* @brief Maximum length per a history.
*/
#define TEXTHISTORY_MAXLEN (NTCONF_EDITOR_MAXLEN)
/**
* @brief Depth of the history.
*/
#define TEXTHISTORY_DEPTH (NTCONF_HISTORY_DEPTH)
/**
* @brief Structure of the text history module.
*/
typedef struct {
/**
* @brief History buffer.
*/
char history[TEXTHISTORY_MAXLEN * TEXTHISTORY_DEPTH];
/**
* @brief Read pointer.
*/
int rp;
/**
* @brief Write pointer.
*/
int wp;
} text_history_t;
#ifdef __cplusplus
extern "C" {
#endif
void text_history_init(text_history_t *p);
int text_history_write(text_history_t *p, char *buf);
int text_history_read(text_history_t *p, char *buf, const int siz);
int text_history_read_point_next(text_history_t *p);
int text_history_read_point_prev(text_history_t *p);
int text_history_find(text_history_t *p, const int index, const char *text, char *buf, const int siz);
#ifdef __cplusplus
}
#endif
#endif

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,130 @@
/**
* @file vtrecv.h
* @author CuBeatSystems
* @author Shinichiro Nakamura
* @copyright
* ===============================================================
* Natural Tiny Shell (NT-Shell) Version 0.3.1
* ===============================================================
* Copyright (c) 2010-2016 Shinichiro Nakamura
*
* 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.
*/
/*
* @note
* An implementation of Paul Williams' DEC compatible state machine parser.
* This code is in the public domain.
*
* @author Joshua Haberman <joshua@reverberate.org>
* @author Shinichiro Nakamura : Modified for Natural Tiny Shell (NT-Shell)
*/
#ifndef VTRECV_H
#define VTRECV_H
/**
* @brief LUTを使うかどうかを決定する
* @details
*
* 15256
* 3840
*
*
*
*
*
*
*
*
*
* @retval 0 使
* @retval 1 使
*/
#define USE_ORIGINAL_LUT (0)
#define MAX_INTERMEDIATE_CHARS 2
#define ACTION(state_change) (vtrecv_action_t)((state_change & 0x0F) >> 0)
#define STATE(state_change) (vtrecv_state_t)((state_change & 0xF0) >> 4)
typedef enum {
VTRECV_STATE_CSI_ENTRY = 1,
VTRECV_STATE_CSI_IGNORE = 2,
VTRECV_STATE_CSI_INTERMEDIATE = 3,
VTRECV_STATE_CSI_PARAM = 4,
VTRECV_STATE_DCS_ENTRY = 5,
VTRECV_STATE_DCS_IGNORE = 6,
VTRECV_STATE_DCS_INTERMEDIATE = 7,
VTRECV_STATE_DCS_PARAM = 8,
VTRECV_STATE_DCS_PASSTHROUGH = 9,
VTRECV_STATE_ESCAPE = 10,
VTRECV_STATE_ESCAPE_INTERMEDIATE = 11,
VTRECV_STATE_GROUND = 12,
VTRECV_STATE_OSC_STRING = 13,
VTRECV_STATE_SOS_PM_APC_STRING = 14,
} vtrecv_state_t;
typedef enum {
VTRECV_ACTION_CLEAR = 1,
VTRECV_ACTION_COLLECT = 2,
VTRECV_ACTION_CSI_DISPATCH = 3,
VTRECV_ACTION_ESC_DISPATCH = 4,
VTRECV_ACTION_EXECUTE = 5,
VTRECV_ACTION_HOOK = 6,
VTRECV_ACTION_IGNORE = 7,
VTRECV_ACTION_OSC_END = 8,
VTRECV_ACTION_OSC_PUT = 9,
VTRECV_ACTION_OSC_START = 10,
VTRECV_ACTION_PARAM = 11,
VTRECV_ACTION_PRINT = 12,
VTRECV_ACTION_PUT = 13,
VTRECV_ACTION_UNHOOK = 14,
VTRECV_ACTION_ERROR = 15,
} vtrecv_action_t;
typedef unsigned char state_change_t;
struct vtrecv;
typedef void (*vtrecv_callback_t)(struct vtrecv*, vtrecv_action_t, unsigned char);
typedef struct vtrecv {
vtrecv_state_t state;
vtrecv_callback_t cb;
unsigned char intermediate_chars[MAX_INTERMEDIATE_CHARS+1];
int num_intermediate_chars;
char ignore_flagged;
int params[16];
int num_params;
void* user_data;
} vtrecv_t;
#ifdef __cplusplus
extern "C" {
#endif
void vtrecv_init(vtrecv_t *parser, vtrecv_callback_t cb);
void vtrecv_execute(vtrecv_t *parser, unsigned char *data, int len);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,270 @@
/**
* @file vtsend.c
* @author CuBeatSystems
* @author Shinichiro Nakamura
* @copyright
* ===============================================================
* Natural Tiny Shell (NT-Shell) Version 0.3.1
* ===============================================================
* Copyright (c) 2010-2016 Shinichiro Nakamura
*
* 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 "vtsend.h"
#define ESC (0x1B)
#define UART_WRITE(P, BUF, SIZ) (P)->uart_write(BUF, SIZ, (P)->extobj)
int vtsend_init(vtsend_t *p, VTSEND_SERIAL_WRITE uart_write, void *extobj)
{
p->uart_write = uart_write;
p->extobj = extobj;
return 0;
}
int vtsend_cursor_position(vtsend_t *p, const int column, const int line)
{
char buf[8];
buf[0] = ESC;
buf[1] = '[';
buf[2] = '0' + (line / 10);
buf[3] = '0' + (line % 10);
buf[4] = ';';
buf[5] = '0' + (column / 10);
buf[6] = '0' + (column % 10);
buf[7] = 'H';
UART_WRITE(p, buf, sizeof(buf));
return 0;
}
int vtsend_cursor_up(vtsend_t *p, const int n)
{
char buf[5];
buf[0] = ESC;
buf[1] = '[';
buf[2] = '0' + (n / 10);
buf[3] = '0' + (n % 10);
buf[4] = 'A';
UART_WRITE(p, buf, sizeof(buf));
return 0;
}
int vtsend_cursor_down(vtsend_t *p, const int n)
{
char buf[5];
buf[0] = ESC;
buf[1] = '[';
buf[2] = '0' + (n / 10);
buf[3] = '0' + (n % 10);
buf[4] = 'B';
UART_WRITE(p, buf, sizeof(buf));
return 0;
}
int vtsend_cursor_forward(vtsend_t *p, const int n)
{
char buf[5];
buf[0] = ESC;
buf[1] = '[';
buf[2] = '0' + (n / 10);
buf[3] = '0' + (n % 10);
buf[4] = 'C';
UART_WRITE(p, buf, sizeof(buf));
return 0;
}
int vtsend_cursor_backward(vtsend_t *p, const int n)
{
char buf[5];
buf[0] = ESC;
buf[1] = '[';
buf[2] = '0' + (n / 10);
buf[3] = '0' + (n % 10);
buf[4] = 'D';
UART_WRITE(p, buf, sizeof(buf));
return 0;
}
int vtsend_cursor_position_save(vtsend_t *p)
{
char buf[3];
buf[0] = ESC;
buf[1] = '[';
buf[2] = 's';
UART_WRITE(p, buf, sizeof(buf));
return 0;
}
int vtsend_cursor_position_restore(vtsend_t *p)
{
char buf[3];
buf[0] = ESC;
buf[1] = '[';
buf[2] = 'u';
UART_WRITE(p, buf, sizeof(buf));
return 0;
}
int vtsend_erase_display(vtsend_t *p)
{
char buf[4];
buf[0] = ESC;
buf[1] = '[';
buf[2] = '2';
buf[3] = 'J';
UART_WRITE(p, buf, sizeof(buf));
return 0;
}
int vtsend_erase_line(vtsend_t *p)
{
char buf[4];
buf[0] = ESC;
buf[1] = '[';
buf[2] = '2';
buf[3] = 'K';
UART_WRITE(p, buf, sizeof(buf));
return 0;
}
int vtsend_set_color_foreground(vtsend_t *p, const int color)
{
char buf[5];
buf[0] = ESC;
buf[1] = '[';
buf[2] = '0' + ((30 + color) / 10);
buf[3] = '0' + ((30 + color) % 10);
buf[4] = 'm';
UART_WRITE(p, buf, sizeof(buf));
return 0;
}
int vtsend_set_color_background(vtsend_t *p, const int color)
{
char buf[5];
buf[0] = ESC;
buf[1] = '[';
buf[2] = '0' + ((40 + color) / 10);
buf[3] = '0' + ((40 + color) % 10);
buf[4] = 'm';
UART_WRITE(p, buf, sizeof(buf));
return 0;
}
int vtsend_set_attribute(vtsend_t *p, const int attr)
{
char buf[5];
buf[0] = ESC;
buf[1] = '[';
buf[2] = '0' + ((attr) / 10);
buf[3] = '0' + ((attr) % 10);
buf[4] = 'm';
UART_WRITE(p, buf, sizeof(buf));
return 0;
}
int vtsend_set_scroll_region(vtsend_t *p, const int top, const int bottom)
{
char buf[8];
buf[0] = ESC;
buf[1] = '[';
buf[2] = '0' + (top / 10);
buf[3] = '0' + (top % 10);
buf[4] = ';';
buf[5] = '0' + (bottom / 10);
buf[6] = '0' + (bottom % 10);
buf[7] = 'r';
UART_WRITE(p, buf, sizeof(buf));
return 0;
}
int vtsend_set_cursor(vtsend_t *p, const int visible)
{
if (visible) {
char buf[6];
buf[0] = ESC;
buf[1] = '[';
buf[2] = '?';
buf[3] = '2';
buf[4] = '5';
buf[5] = 'h';
UART_WRITE(p, buf, sizeof(buf));
} else {
char buf[6];
buf[0] = ESC;
buf[1] = '[';
buf[2] = '?';
buf[3] = '2';
buf[4] = '5';
buf[5] = 'l';
UART_WRITE(p, buf, sizeof(buf));
}
return 0;
}
int vtsend_reset(vtsend_t *p)
{
char buf[2];
buf[0] = ESC;
buf[1] = 'c';
UART_WRITE(p, buf, sizeof(buf));
return 0;
}
int vtsend_draw_box(
vtsend_t *p,
const int x1, const int y1, const int x2, const int y2)
{
int i;
vtsend_cursor_position(p, x1, y1);
for (i = x1; i <= x2; i++) {
UART_WRITE(p, " ", 1);
}
vtsend_cursor_position(p, x1, y2);
for (i = x1; i <= x2; i++) {
UART_WRITE(p, " ", 1);
}
for (i = y1; i <= y2; i++) {
vtsend_cursor_position(p, x1, i);
UART_WRITE(p, " ", 1);
vtsend_cursor_position(p, x2, i);
UART_WRITE(p, " ", 1);
}
return 0;
}
int vtsend_fill_box(
vtsend_t *p,
const int x1, const int y1, const int x2, const int y2)
{
int i, j;
for (i = y1; i <= y2; i++) {
vtsend_cursor_position(p, x1, i);
for (j = x1; j <= x2; j++) {
UART_WRITE(p, " ", 1);
}
}
return 0;
}

View File

@ -0,0 +1,92 @@
/**
* @file vtsend.h
* @author CuBeatSystems
* @author Shinichiro Nakamura
* @copyright
* ===============================================================
* Natural Tiny Shell (NT-Shell) Version 0.3.1
* ===============================================================
* Copyright (c) 2010-2016 Shinichiro Nakamura
*
* 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 VTSEND_H
#define VTSEND_H
#define VTSEND_COLOR_BLACK (0)
#define VTSEND_COLOR_RED (1)
#define VTSEND_COLOR_GREEN (2)
#define VTSEND_COLOR_YELLOW (3)
#define VTSEND_COLOR_BLUE (4)
#define VTSEND_COLOR_MAGENTA (5)
#define VTSEND_COLOR_CYAN (6)
#define VTSEND_COLOR_WHITE (7)
#define VTSEND_ATTR_OFF (0)
#define VTSEND_ATTR_BOLD_ON (1)
#define VTSEND_ATTR_UNDERSCORE (4)
#define VTSEND_ATTR_BLINK_ON (5)
#define VTSEND_ATTR_REVERSE (7)
#define VTSEND_ATTR_CONCEALED_ON (8)
typedef int (*VTSEND_SERIAL_WRITE)(const char *buf, const int siz, void *extobj);
typedef struct {
VTSEND_SERIAL_WRITE uart_write;
void *extobj;
} vtsend_t;
#ifdef __cplusplus
extern "C" {
#endif
int vtsend_init(vtsend_t *p, VTSEND_SERIAL_WRITE uart_write, void *extobj);
int vtsend_cursor_position(vtsend_t *p, const int column, const int line);
int vtsend_cursor_up(vtsend_t *p, const int n);
int vtsend_cursor_down(vtsend_t *p, const int n);
int vtsend_cursor_forward(vtsend_t *p, const int n);
int vtsend_cursor_backward(vtsend_t *p, const int n);
int vtsend_cursor_position_save(vtsend_t *p);
int vtsend_cursor_position_restore(vtsend_t *p);
int vtsend_erase_display(vtsend_t *p);
int vtsend_erase_line(vtsend_t *p);
int vtsend_set_color_foreground(vtsend_t *p, const int color);
int vtsend_set_color_background(vtsend_t *p, const int color);
int vtsend_set_attribute(vtsend_t *p, const int attr);
int vtsend_set_scroll_region(vtsend_t *p, const int top, const int bottom);
int vtsend_set_cursor(vtsend_t *p, const int visible);
int vtsend_reset(vtsend_t *p);
int vtsend_draw_box(
vtsend_t *p,
const int x1, const int y1, const int x2, const int y2);
int vtsend_fill_box(
vtsend_t *p,
const int x1, const int y1, const int x2, const int y2);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,156 @@
/**
* @file ntopt.c
* @author CuBeatSystems
* @author Shinichiro Nakamura
* @copyright
* ===============================================================
* Natural Tiny Shell (NT-Shell) Version 0.3.1
* ===============================================================
* Copyright (c) 2010-2016 Shinichiro Nakamura
*
* 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 "ntopt.h"
/**
* @brief Is the character delimiter?
* @param c The character.
* @retval true It's a delimiter.
* @retval false It's not a delimiter.
*/
#define IS_DELIM(c) \
(((c) == '\r') || ((c) == '\n') || ((c) == '\t') || ((c) == '\0') || ((c) == ' '))
static int ntopt_get_count(const char *str);
static char *ntopt_get_text(
const char *str, const int n, char *buf, int siz, int *len);
/**
* @brief Get the sentence count of the given text string.
* @param str A text string.
* @return Count of the given sentence.
*/
static int ntopt_get_count(const char *str)
{
int cnt = 0;
int wc = 0;
char *p = (char *)str;
while (*p) {
if (!IS_DELIM(*p)) {
wc++;
if (wc == 1) {
cnt++;
}
} else {
wc = 0;
}
p++;
}
return cnt;
}
/**
* @brief Get the sentence of the given text string.
* @param str A text string.
* @param n Index number. (0 to ntopt-get_count(str) - 1)
* @param buf The pointer to a stored buffer.
* @param siz The size of the stored buffer.
* @param len The stored string length.
* @retval !NULL Success. The pointer to the buffer.
* @retval NULL Failure.
*/
static char *ntopt_get_text(
const char *str, const int n, char *buf, int siz, int *len)
{
int cnt = 0;
int wc = 0;
char *p = (char *)str;
*len = 0;
while (*p) {
if (!IS_DELIM(*p)) {
wc++;
if (wc == 1) {
if (cnt == n) {
char *des = buf;
int cc = 0;
while (!IS_DELIM(*p)) {
cc++;
if (siz <= cc) {
break;
}
*des = *p;
des++;
p++;
}
*des = '\0';
*len = cc;
return buf;
}
cnt++;
}
} else {
wc = 0;
}
p++;
}
return (char *)0;
}
/**
* @brief Parse the given text string.
* @param str A text string.
* @param func The callback function.
* @param extobj An external object for the callback function.
* @return The return value from the callback.
*/
int ntopt_parse(const char *str, NTOPT_CALLBACK func, void *extobj)
{
int argc;
char argv[NTOPT_TEXT_MAXLEN];
char *argvp[NTOPT_TEXT_MAXARGS];
int i;
int total;
char *p;
argc = ntopt_get_count(str);
if (NTOPT_TEXT_MAXARGS <= argc) {
argc = NTOPT_TEXT_MAXARGS;
}
total = 0;
p = &argv[0];
for (i = 0; i < argc; i++) {
int len;
argvp[i] = ntopt_get_text(
str, i, p, NTOPT_TEXT_MAXLEN - total, &len);
if (total + len + 1 < NTOPT_TEXT_MAXLEN) {
p += len + 1;
total += len + 1;
} else {
break;
}
}
return func(argc, &argvp[0], extobj);
}

View File

@ -0,0 +1,70 @@
/**
* @file ntopt.h
* @author CuBeatSystems
* @author Shinichiro Nakamura
* @copyright
* ===============================================================
* Natural Tiny Shell (NT-Shell) Version 0.3.1
* ===============================================================
* Copyright (c) 2010-2016 Shinichiro Nakamura
*
* 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 NTOPT_H
#define NTOPT_H
#include "../core/ntconf.h"
/**
* @brief The text maximum length.
*/
#define NTOPT_TEXT_MAXLEN (NTCONF_EDITOR_MAXLEN)
/**
* @brief The text maximum arguments.
*/
#define NTOPT_TEXT_MAXARGS (NTCONF_EDITOR_MAXLEN / 2)
/**
* @brief The callback function.
*
* @param argc The number of the parameters.
* @param argv The pointer to the parameters.
* @param extobj The external object.
*
* @return A return value.
*/
typedef int (*NTOPT_CALLBACK)(int argc, char **argv, void *extobj);
#ifdef __cplusplus
extern "C" {
#endif
int ntopt_parse(const char *str, NTOPT_CALLBACK func, void *extobj);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,364 @@
/**
* @file ntstdio.c
* @author CuBeatSystems
* @author Shinichiro Nakamura
* @copyright
* ===============================================================
* Natural Tiny Shell (NT-Shell) Version 0.3.1
* ===============================================================
* Copyright (c) 2010-2016 Shinichiro Nakamura
*
* 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.
*/
/*------------------------------------------------------------------------/
/ Universal string handler for user console interface
/-------------------------------------------------------------------------/
/
/ Copyright (C) 2011, ChaN, all right reserved.
/
/ * This software is a free software and there is NO WARRANTY.
/ * No restriction on use. You can use, modify and redistribute it for
/ personal, non-profit or commercial products UNDER YOUR RESPONSIBILITY.
/ * Redistributions of source code must retain the above copyright notice.
/
/-------------------------------------------------------------------------*/
#include <stdarg.h>
#include "ntstdio.h"
#define FLAG_ZERO_PADDED (1 << 0)
#define FLAG_LEFT_JUSTIFIED (1 << 1)
#define FLAG_SIZE_LONG_INT (1 << 2)
#define FLAG_SIGNED_DECIMAL (1 << 3)
/*
* ntstdio_printf("%d", 1234); "1234"
* ntstdio_printf("%6d,%3d%%", -200, 5); " -200, 5%"
* ntstdio_printf("%-6u", 100); "100 "
* ntstdio_printf("%ld", 12345678L); "12345678"
* ntstdio_printf("%04x", 0xA3); "00a3"
* ntstdio_printf("%08LX", 0x123ABC); "00123ABC"
* ntstdio_printf("%016b", 0x550F); "0101010100001111"
* ntstdio_printf("%s", "String"); "String"
* ntstdio_printf("%-4s", "abc"); "abc "
* ntstdio_printf("%4s", "abc"); " abc"
* ntstdio_printf("%c", 'a'); "a"
* ntstdio_printf("%f", 10.0); <ntstdio_printf lacks floating point support>
*/
static void xvprintf(ntstdio_t *handle, const char *fmt, va_list arp)
{
unsigned int i, j;
unsigned int flag, radix, width;
unsigned long value;
char s[16], c, d, *p;
while (1) {
/*
* Get a character.
*/
c = *fmt++;
if (!c) {
/*
* End of the format.
*/
break;
}
if (c != '%') {
/*
* Pass through it if not a % sequense
*/
ntstdio_putc(handle, c);
continue;
}
/*
* Reset the flag.
*/
flag = 0;
/*
* Get the first character of the sequense.
*/
c = *fmt++;
if (c == '0') {
flag = FLAG_ZERO_PADDED;
c = *fmt++;
} else {
if (c == '-') {
flag = FLAG_LEFT_JUSTIFIED;
c = *fmt++;
}
}
/*
* Calculate the minimum width.
*/
for (width = 0; (c >= '0') && (c <= '9'); c = *fmt++) {
width = (width * 10) + (c - '0');
}
if ((c == 'l') || (c == 'L')) {
flag |= FLAG_SIZE_LONG_INT;
c = *fmt++;
}
if (!c) {
/*
* End of the format.
*/
break;
}
d = c;
if (d >= 'a') {
d -= 0x20;
}
/* Type is... */
switch (d) {
case 'S' :
/* String */
p = va_arg(arp, char*);
for (j = 0; p[j]; j++) {
}
while (!(flag & FLAG_LEFT_JUSTIFIED) && (j++ < width)) {
ntstdio_putc(handle, ' ');
}
ntstdio_puts(handle, p);
while (j++ < width) {
ntstdio_putc(handle, ' ');
}
continue;
case 'C' :
/* Character */
ntstdio_putc(handle, (char)va_arg(arp, int));
continue;
case 'B' :
/* Binary */
radix = 2;
break;
case 'O' :
/* Octal */
radix = 8;
break;
case 'D' :
/* Signed decimal */
radix = 10;
break;
case 'U' :
/* Unsigned decimal */
radix = 10;
break;
case 'X' :
/* Hexdecimal */
radix = 16;
break;
default:
/* Unknown type (passthrough) */
ntstdio_putc(handle, c);
continue;
}
/*
* Get an argument and put it in numeral.
*/
value = (flag & FLAG_SIZE_LONG_INT) ? va_arg(arp, long) : ((d == 'D') ? (long)va_arg(arp, int) : (long)va_arg(arp, unsigned int));
if ((d == 'D') && (value & 0x80000000)) {
value = 0 - value;
flag |= FLAG_SIGNED_DECIMAL;
}
i = 0;
do {
d = (char)(value % radix);
value /= radix;
if (d > 9) {
d += (c == 'x') ? 0x27 : 0x07;
}
s[i++] = d + '0';
} while (value && (i < sizeof(s)));
if (flag & FLAG_SIGNED_DECIMAL) {
s[i++] = '-';
}
j = i;
d = (flag & FLAG_ZERO_PADDED) ? '0' : ' ';
while (!(flag & FLAG_LEFT_JUSTIFIED) && (j++ < width)) {
ntstdio_putc(handle, d);
}
do {
ntstdio_putc(handle, s[--i]);
} while(i);
while (j++ < width) {
ntstdio_putc(handle, ' ');
}
}
}
void ntstdio_init(ntstdio_t *handle, unsigned int option, NTSTDIO_XI xi, NTSTDIO_XO xo)
{
handle->xi = xi;
handle->xo = xo;
handle->outptr = 0;
handle->option = option;
}
void ntstdio_putc(ntstdio_t *handle, char c)
{
if ((handle->option & NTSTDIO_OPTION_CR_CRLF) && (c == '\n')) {
ntstdio_putc(handle, '\r');
}
if (handle->outptr) {
*(handle->outptr)++ = (unsigned char)c;
return;
}
if (handle->xo) {
handle->xo((unsigned char)c);
}
}
void ntstdio_puts(ntstdio_t *handle, const char *str)
{
while (*str) {
ntstdio_putc(handle, *str++);
}
}
void ntstdio_fputs(ntstdio_t *handle, NTSTDIO_XO xo, const char *str)
{
void (*pf)(unsigned char);
/* Save current output device */
pf = handle->xo;
/* Switch output to specified device */
handle->xo = xo;
while (*str) {
ntstdio_putc(handle, *str++);
}
/* Restore output device */
handle->xo = pf;
}
void ntstdio_printf(ntstdio_t *handle, const char *fmt, ...)
{
va_list arp;
va_start(arp, fmt);
xvprintf(handle, fmt, arp);
va_end(arp);
}
void ntstdio_sprintf(ntstdio_t *handle, char *buf, const char *fmt, ...)
{
va_list arp;
/* Switch destination for memory */
handle->outptr = buf;
va_start(arp, fmt);
xvprintf(handle, fmt, arp);
va_end(arp);
/* Terminate output string with a \0 */
*(handle->outptr) = 0;
/* Switch destination for device */
handle->outptr = 0;
}
void ntstdio_fprintf(ntstdio_t *handle, NTSTDIO_XO xo, const char *fmt, ...)
{
va_list arp;
void (*pf)(unsigned char);
/* Save current output device */
pf = handle->xo;
/* Switch output to specified device */
handle->xo = xo;
va_start(arp, fmt);
xvprintf(handle, fmt, arp);
va_end(arp);
/* Restore output device */
handle->xo = pf;
}
/* 0:End of stream, 1:A line arrived */
int ntstdio_gets(ntstdio_t *handle, char *buf, int len)
{
int c, i;
if (!handle->xi) {
/* No input function specified */
return 0;
}
i = 0;
for (;;) {
/* Get a char from the incoming stream */
c = handle->xi();
if (!c) {
/* End of stream */
return 0;
}
if (c == '\r') {
/* End of line */
break;
}
if ((c == '\b') && i) {
/* Back space */
i--;
if (handle->option & NTSTDIO_OPTION_LINE_ECHO) {
ntstdio_putc(handle, c);
}
continue;
}
if ((c >= ' ') && (i < len - 1)) {
/* Visible chars */
buf[i++] = c;
if (handle->option & NTSTDIO_OPTION_LINE_ECHO) {
ntstdio_putc(handle, c);
}
}
}
buf[i] = 0;
/* Terminate with a \0 */
if (handle->option & NTSTDIO_OPTION_LINE_ECHO) {
ntstdio_putc(handle, '\n');
}
return 1;
}
/* 0:End of stream, 1:A line arrived */
int ntstdio_fgets(ntstdio_t *handle, NTSTDIO_XI xi, char *buf, int len)
{
unsigned char (*pf)(void);
int n;
/* Save current input device */
pf = handle->xi;
/* Switch input to specified device */
handle->xi = xi;
/* Get a line */
n = ntstdio_gets(handle, buf, len);
/* Restore input device */
handle->xi = pf;
return n;
}

View File

@ -0,0 +1,82 @@
/**
* @file ntstdio.h
* @author CuBeatSystems
* @author Shinichiro Nakamura
* @copyright
* ===============================================================
* Natural Tiny Shell (NT-Shell) Version 0.3.1
* ===============================================================
* Copyright (c) 2010-2016 Shinichiro Nakamura
*
* 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.
*/
/*------------------------------------------------------------------------/
/ Universal string handler for user console interface
/-------------------------------------------------------------------------/
/
/ Copyright (C) 2011, ChaN, all right reserved.
/
/ * This software is a free software and there is NO WARRANTY.
/ * No restriction on use. You can use, modify and redistribute it for
/ personal, non-profit or commercial products UNDER YOUR RESPONSIBILITY.
/ * Redistributions of source code must retain the above copyright notice.
/
/-------------------------------------------------------------------------*/
#ifndef NTSTDIO_H
#define NTSTDIO_H
/**
* 1: Convert \n ==> \r\n in the output char.
*/
#define NTSTDIO_OPTION_CR_CRLF (1 << 0)
/**
* 1: Echo back input chars in ntstdio_gets function.
*/
#define NTSTDIO_OPTION_LINE_ECHO (1 << 1)
typedef unsigned char (*NTSTDIO_XI)(void);
typedef void (*NTSTDIO_XO)(unsigned char c);
typedef struct {
NTSTDIO_XI xi;
NTSTDIO_XO xo;
char *outptr;
unsigned int option;
} ntstdio_t;
void ntstdio_init(ntstdio_t *handle, unsigned int option, NTSTDIO_XI xi, NTSTDIO_XO xo);
void ntstdio_putc(ntstdio_t *handle, char c);
void ntstdio_puts(ntstdio_t *handle, const char *str);
void ntstdio_fputs(ntstdio_t *handle, NTSTDIO_XO xo, const char *str);
void ntstdio_printf(ntstdio_t *handle, const char *fmt, ...);
void ntstdio_sprintf(ntstdio_t *handle, char *buf, const char *fmt, ...);
void ntstdio_fprintf(ntstdio_t *handle, NTSTDIO_XO xo, const char *fmt, ...);
int ntstdio_gets(ntstdio_t *handle, char *buf, int len);
int ntstdio_fgets(ntstdio_t *handle, NTSTDIO_XI xi, char *buf, int len);
#endif

View File

@ -15,7 +15,7 @@
* *
* - implement config storage * - implement config storage
* *
* - implement USB CDC commmand shell * - implement USB CDC command shell
* *
* - when USB is not active, go into super low power state * - when USB is not active, go into super low power state
* - light sensor is only checked once every two seconds or so * - light sensor is only checked once every two seconds or so
@ -33,6 +33,7 @@
#include "periph/usb/cdc.h" #include "periph/usb/cdc.h"
#include "usb_lib.h" #include "usb_lib.h"
#include "console/console.h"
@ -194,8 +195,17 @@ int main(void)
USB_Init(); USB_Init();
usb_intr_init(); usb_intr_init();
console_init();
// let's do this // let's do this
while (1) { while (1) {
// todo: add check to see if USB is connected
if (1) {
console_process();
} else {
console_stop();
}
__WFI(); __WFI();
} }
} }

View File

@ -0,0 +1,17 @@
/*
* version.h
*
* Created on: Nov 6, 2024
* Author: true
*/
#ifndef USER_VERSION_H_
#define USER_VERSION_H_
#define FW_VERSION "20241106.0"
#endif /* USER_VERSION_H_ */