API
The application interfaces of NT-Shell are really simple.
Basically, you need to use only two functions such as ntshell_init and ntshell_execute.
For the porting to a embdded system, it needs only two serial I/O interface functions (read and write).
The core functions
void ntshell_init(
ntshell_t *p,
NTSHELL_SERIAL_READ func_read,
NTSHELL_SERIAL_WRITE func_write,
NTSHELL_USER_CALLBACK func_callback,
void *extobj);
void ntshell_execute(ntshell_t *p);
The utilities
void ntshell_set_prompt(ntshell_t *p, const char *prompt);
void ntshell_version(int *major, int *minor, int *release);
The user functions
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);
Examples
#include "ntshell.h"
#include "chip.h"
#include "uart.h"
#include "ntlibc.h"
#define UNUSED_VARIABLE(N) do { (void)(N); } while (0)
static int func_read(char *buf, int cnt, void *extobj)
{
int i;
UNUSED_VARIABLE(extobj);
for (i = 0; i < cnt; i++) {
buf[i] = uart_getc();
}
return cnt;
}
static int func_write(const char *buf, int cnt, void *extobj)
{
int i;
UNUSED_VARIABLE(extobj);
for (i = 0; i < cnt; i++) {
uart_putc(buf[i]);
}
return cnt;
}
static int func_callback(const char *text, void *extobj)
{
ntshell_t *ntshell = (ntshell_t *)extobj;
UNUSED_VARIABLE(ntshell);
UNUSED_VARIABLE(extobj);
if (ntlibc_strlen(text) > 0) {
uart_puts("User input text:'");
uart_puts(text);
uart_puts("'\r\n");
}
return 0;
}
int main(void)
{
ntshell_t ntshell;
chip_init();
uart_init();
ntshell_init(
&ntshell,
func_read,
func_write,
func_callback,
(void *)&ntshell);
ntshell_set_prompt(&ntshell, "BlueTank>");
ntshell_execute(&ntshell);
return 0;
}