4
0
mirror of https://github.com/RT-Thread/rt-thread.git synced 2025-01-18 17:03:30 +08:00

[finsh] Use standard uint type for data type.

Use uint8_t/uint32_t etc to replace u_char/u_long etc;
Use getchar for shell input.
This commit is contained in:
bernard 2017-10-10 14:27:34 +08:00
parent 9a131b9452
commit 95ab8c02aa
19 changed files with 293 additions and 309 deletions

View File

@ -6,6 +6,10 @@ config RT_USING_FINSH
if RT_USING_FINSH if RT_USING_FINSH
config FINSH_USING_HISTORY
bool "Enable command history feature"
default y
config FINSH_USING_SYMTAB config FINSH_USING_SYMTAB
bool "Using symbol table for commands" bool "Using symbol table for commands"
default y default y
@ -18,18 +22,25 @@ config FINSH_THREAD_STACK_SIZE
int "The stack size for finsh thread" int "The stack size for finsh thread"
default 4096 default 4096
config FINSH_CMD_SIZE
int "The command line size for shell"
default 80
config FINSH_USING_AUTH config FINSH_USING_AUTH
bool "shell support authentication" bool "shell support authentication"
default n default n
if FINSH_USING_AUTH
config FINSH_DEFAULT_PASSWORD config FINSH_DEFAULT_PASSWORD
string "The default password for shell authentication" string "The default password for shell authentication"
default "rtthread" default "rtthread"
endif
config FINSH_USING_MSH config FINSH_USING_MSH
bool "Using module shell" bool "Using module shell"
default y default y
if FINSH_USING_MSH
config FINSH_USING_MSH_DEFAULT config FINSH_USING_MSH_DEFAULT
bool "Using module shell in default" bool "Using module shell in default"
default y default y
@ -37,6 +48,7 @@ config FINSH_USING_MSH_DEFAULT
config FINSH_USING_MSH_ONLY config FINSH_USING_MSH_ONLY
bool "Only using module shell" bool "Only using module shell"
default n default n
endif
endif endif

View File

@ -121,13 +121,15 @@ static long _list_thread(struct rt_list_node *list)
rt_kprintf( " --- ------- ---------- ---------- ------ ---------- ---\n"); rt_kprintf( " --- ------- ---------- ---------- ------ ---------- ---\n");
for (node = list->next; node != list; node = node->next) for (node = list->next; node != list; node = node->next)
{ {
rt_uint8_t stat;
thread = rt_list_entry(node, struct rt_thread, list); thread = rt_list_entry(node, struct rt_thread, list);
rt_kprintf("%-*.*s %3d ", maxlen, RT_NAME_MAX, thread->name, thread->current_priority); rt_kprintf("%-*.*s %3d ", maxlen, RT_NAME_MAX, thread->name, thread->current_priority);
if (thread->stat == RT_THREAD_READY) rt_kprintf(" ready "); stat = (thread->stat & RT_THREAD_STAT_MASK);
else if (thread->stat == RT_THREAD_SUSPEND) rt_kprintf(" suspend"); if (stat == RT_THREAD_READY) rt_kprintf(" ready ");
else if (thread->stat == RT_THREAD_INIT) rt_kprintf(" init "); else if (stat == RT_THREAD_SUSPEND) rt_kprintf(" suspend");
else if (thread->stat == RT_THREAD_CLOSE) rt_kprintf(" close "); else if (stat == RT_THREAD_INIT) rt_kprintf(" init ");
else if (stat == RT_THREAD_CLOSE) rt_kprintf(" close ");
ptr = (rt_uint8_t *)thread->stack_addr; ptr = (rt_uint8_t *)thread->stack_addr;
while (*ptr == '#')ptr ++; while (*ptr == '#')ptr ++;
@ -613,14 +615,17 @@ int list_mod_detail(const char *name)
/* list main thread in module */ /* list main thread in module */
if (module->module_thread != RT_NULL) if (module->module_thread != RT_NULL)
{ {
rt_uint8_t stat;
rt_kprintf("main thread pri status sp stack size max used left tick error\n"); rt_kprintf("main thread pri status sp stack size max used left tick error\n");
rt_kprintf("------------- ---- ------- ---------- ---------- ---------- ---------- ---\n"); rt_kprintf("------------- ---- ------- ---------- ---------- ---------- ---------- ---\n");
thread = module->module_thread; thread = module->module_thread;
rt_kprintf("%-8.*s 0x%02x", RT_NAME_MAX, thread->name, thread->current_priority); rt_kprintf("%-8.*s 0x%02x", RT_NAME_MAX, thread->name, thread->current_priority);
if (thread->stat == RT_THREAD_READY) rt_kprintf(" ready "); stat = (thread->stat & RT_THREAD_STAT_MASK);
else if (thread->stat == RT_THREAD_SUSPEND) rt_kprintf(" suspend"); if (stat == RT_THREAD_READY) rt_kprintf(" ready ");
else if (thread->stat == RT_THREAD_INIT) rt_kprintf(" init "); else if (stat == RT_THREAD_SUSPEND) rt_kprintf(" suspend");
else if (stat == RT_THREAD_INIT) rt_kprintf(" init ");
ptr = (rt_uint8_t *)thread->stack_addr; ptr = (rt_uint8_t *)thread->stack_addr;
while (*ptr == '#')ptr ++; while (*ptr == '#')ptr ++;

View File

@ -56,38 +56,12 @@
/* -- the end of option -- */ /* -- the end of option -- */
#if defined(RT_USING_NEWLIB) || defined (RT_USING_MINILIBC) /* std header file */
#include <sys/types.h>
#include <string.h>
#else
typedef unsigned char u_char;
typedef unsigned short u_short;
typedef unsigned long u_long;
#if !defined(__CC_ARM) && \
!defined(__IAR_SYSTEMS_ICC__) && \
!defined(__ADSPBLACKFIN__) && \
!defined(_MSC_VER)
/* only for GNU GCC */
#if !(defined(__GNUC__) && defined(__x86_64__))
typedef unsigned int size_t;
#else
#include <stdio.h> #include <stdio.h>
#endif
#ifndef NULL
#define NULL RT_NULL
#endif
#else
/* use libc of armcc */
#include <ctype.h> #include <ctype.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h>
#include <string.h> #include <string.h>
#endif
#endif
#define FINSH_VERSION_MAJOR 1 #define FINSH_VERSION_MAJOR 1
#define FINSH_VERSION_MINOR 0 #define FINSH_VERSION_MINOR 0
@ -144,7 +118,7 @@ struct finsh_sysvar
#if defined(FINSH_USING_DESCRIPTION) && defined(FINSH_USING_SYMTAB) #if defined(FINSH_USING_DESCRIPTION) && defined(FINSH_USING_SYMTAB)
const char* desc; /* description of system variable */ const char* desc; /* description of system variable */
#endif #endif
u_char type; /* the type of variable */ uint8_t type; /* the type of variable */
void* var ; /* the address of variable */ void* var ; /* the address of variable */
}; };
@ -360,16 +334,16 @@ struct finsh_token
char replay; char replay;
int position; int position;
u_char current_token; uint8_t current_token;
union { union {
char char_value; char char_value;
int int_value; int int_value;
long long_value; long long_value;
} value; } value;
u_char string[FINSH_STRING_MAX]; uint8_t string[FINSH_STRING_MAX];
u_char* line; uint8_t* line;
}; };
#define FINSH_IDTYPE_VAR 0x01 #define FINSH_IDTYPE_VAR 0x01
@ -378,9 +352,9 @@ struct finsh_token
#define FINSH_IDTYPE_ADDRESS 0x08 #define FINSH_IDTYPE_ADDRESS 0x08
struct finsh_node struct finsh_node
{ {
u_char node_type; /* node node_type */ uint8_t node_type; /* node node_type */
u_char data_type; /* node data node_type */ uint8_t data_type; /* node data node_type */
u_char idtype; /* id node information */ uint8_t idtype; /* id node information */
union { /* value node */ union { /* value node */
char char_value; char char_value;
@ -403,7 +377,7 @@ struct finsh_node
struct finsh_parser struct finsh_parser
{ {
u_char* parser_string; uint8_t* parser_string;
struct finsh_token token; struct finsh_token token;
struct finsh_node* root; struct finsh_node* root;
@ -455,9 +429,9 @@ struct finsh_var* finsh_var_lookup(const char* name);
long finsh_stack_bottom(void); long finsh_stack_bottom(void);
/* get error number of finsh */ /* get error number of finsh */
u_char finsh_errno(void); uint8_t finsh_errno(void);
/* get error string */ /* get error string */
const char* finsh_error_string(u_char type); const char* finsh_error_string(uint8_t type);
#ifdef RT_USING_HEAP #ifdef RT_USING_HEAP
/** /**
@ -477,6 +451,6 @@ void finsh_syscall_append(const char* name, syscall_func func);
* @param type the data type of system variable * @param type the data type of system variable
* @param addr the address of system variable * @param addr the address of system variable
*/ */
void finsh_sysvar_append(const char* name, u_char type, void* addr); void finsh_sysvar_append(const char* name, uint8_t type, void* addr);
#endif #endif
#endif #endif

View File

@ -34,7 +34,7 @@
#include "finsh_ops.h" #include "finsh_ops.h"
union finsh_value* finsh_compile_sp; /* stack pointer */ union finsh_value* finsh_compile_sp; /* stack pointer */
u_char* finsh_compile_pc; /* PC */ uint8_t* finsh_compile_pc; /* PC */
#define finsh_code_byte(x) do { *finsh_compile_pc = (x); finsh_compile_pc ++; } while(0) #define finsh_code_byte(x) do { *finsh_compile_pc = (x); finsh_compile_pc ++; } while(0)
#define finsh_code_word(x) do { FINSH_SET16(finsh_compile_pc, x); finsh_compile_pc +=2; } while(0) #define finsh_code_word(x) do { FINSH_SET16(finsh_compile_pc, x); finsh_compile_pc +=2; } while(0)
@ -210,7 +210,7 @@ static int finsh_compile(struct finsh_node* node)
case FINSH_NODE_VALUE_NULL: case FINSH_NODE_VALUE_NULL:
case FINSH_NODE_VALUE_STRING: case FINSH_NODE_VALUE_STRING:
finsh_code_byte(FINSH_OP_LD_DWORD); finsh_code_byte(FINSH_OP_LD_DWORD);
finsh_code_dword((u_long)node->value.ptr); finsh_code_dword((uint32_t)node->value.ptr);
break; break;
/* arithmetic operation */ /* arithmetic operation */
@ -756,7 +756,7 @@ static int finsh_compile(struct finsh_node* node)
return 0; return 0;
} }
static int finsh_type_check(struct finsh_node* node, u_char is_addr) static int finsh_type_check(struct finsh_node* node, uint8_t is_addr)
{ {
if (node != NULL) if (node != NULL)
{ {

View File

@ -28,7 +28,7 @@
*/ */
#include "finsh_error.h" #include "finsh_error.h"
u_char global_errno; uint8_t global_errno;
static const char * finsh_error_string_table[] = static const char * finsh_error_string_table[] =
{ {
@ -56,19 +56,19 @@ int finsh_error_init()
return 0; return 0;
} }
int finsh_error_set(u_char type) int finsh_error_set(uint8_t type)
{ {
global_errno = type; global_errno = type;
return 0; return 0;
} }
u_char finsh_errno() uint8_t finsh_errno()
{ {
return global_errno; return global_errno;
} }
const char* finsh_error_string(u_char type) const char* finsh_error_string(uint8_t type)
{ {
return finsh_error_string_table[type]; return finsh_error_string_table[type];
} }

View File

@ -34,9 +34,9 @@
int finsh_error_init(void); int finsh_error_init(void);
/* get error number */ /* get error number */
u_char finsh_errno(void); uint8_t finsh_errno(void);
int finsh_error_set(u_char type); int finsh_error_set(uint8_t type);
const char* finsh_error_string(u_char type); const char* finsh_error_string(uint8_t type);
#endif #endif

View File

@ -31,15 +31,15 @@
#include "finsh_var.h" #include "finsh_var.h"
ALIGN(RT_ALIGN_SIZE) ALIGN(RT_ALIGN_SIZE)
u_char finsh_heap[FINSH_HEAP_MAX]; uint8_t finsh_heap[FINSH_HEAP_MAX];
struct finsh_block_header struct finsh_block_header
{ {
u_long length; uint32_t length;
struct finsh_block_header* next; struct finsh_block_header* next;
}; };
#define BLOCK_HEADER(x) (struct finsh_block_header*)(x) #define BLOCK_HEADER(x) (struct finsh_block_header*)(x)
#define finsh_block_get_header(data) (struct finsh_block_header*)((u_char*)data - sizeof(struct finsh_block_header)) #define finsh_block_get_header(data) (struct finsh_block_header*)((uint8_t*)data - sizeof(struct finsh_block_header))
#define finsh_block_get_data(header) (u_char*)((struct finsh_block_header*)header + 1) #define finsh_block_get_data(header) (uint8_t*)((struct finsh_block_header*)header + 1)
#define HEAP_ALIGN_SIZE(size) (((size) + HEAP_ALIGNMENT - 1) & ~(HEAP_ALIGNMENT-1)) #define HEAP_ALIGN_SIZE(size) (((size) + HEAP_ALIGNMENT - 1) & ~(HEAP_ALIGNMENT-1))
static struct finsh_block_header* free_list; static struct finsh_block_header* free_list;
@ -237,7 +237,7 @@ void finsh_block_split(struct finsh_block_header* header, size_t size)
* split header into two node: * split header into two node:
* header->next->... * header->next->...
*/ */
next = BLOCK_HEADER((u_char*)header + sizeof(struct finsh_block_header) + size); next = BLOCK_HEADER((uint8_t*)header + sizeof(struct finsh_block_header) + size);
next->length = header->length - sizeof(struct finsh_block_header) - size; next->length = header->length - sizeof(struct finsh_block_header) - size;
header->length = size; header->length = size;
next->next = header->next; next->next = header->next;
@ -267,13 +267,13 @@ void finsh_block_merge(struct finsh_block_header** list, struct finsh_block_head
/* merge to previous node */ /* merge to previous node */
if (prev_node != NULL && if (prev_node != NULL &&
((u_char*)prev_node + prev_node->length + sizeof(struct finsh_block_header) ((uint8_t*)prev_node + prev_node->length + sizeof(struct finsh_block_header)
== (u_char*)header)) == (uint8_t*)header))
{ {
/* is it close to next node? */ /* is it close to next node? */
if ((next_node != NULL) && if ((next_node != NULL) &&
((u_char*)header + header->length + sizeof(struct finsh_block_header) ((uint8_t*)header + header->length + sizeof(struct finsh_block_header)
== (u_char*)next_node)) == (uint8_t*)next_node))
{ {
/* merge three node */ /* merge three node */
prev_node->length += header->length + next_node->length + prev_node->length += header->length + next_node->length +
@ -289,8 +289,8 @@ void finsh_block_merge(struct finsh_block_header** list, struct finsh_block_head
} }
else /* merge to last node */ else /* merge to last node */
if ( (next_node != NULL) && if ( (next_node != NULL) &&
((u_char*)header + header->length + sizeof(struct finsh_block_header) ((uint8_t*)header + header->length + sizeof(struct finsh_block_header)
== (u_char*)next_node)) == (uint8_t*)next_node))
{ {
header->length += next_node->length + sizeof(struct finsh_block_header); header->length += next_node->length + sizeof(struct finsh_block_header);
header->next = next_node->next; header->next = next_node->next;

View File

@ -42,7 +42,7 @@ int finsh_node_init()
return 0; return 0;
} }
struct finsh_node* finsh_node_allocate(u_char type) struct finsh_node* finsh_node_allocate(uint8_t type)
{ {
int i; int i;
@ -181,7 +181,7 @@ struct finsh_node* finsh_node_new_string(char* s)
/* make string */ /* make string */
node->value.ptr = finsh_heap_allocate(strlen(s) + 1); node->value.ptr = finsh_heap_allocate(strlen(s) + 1);
strncpy(node->value.ptr, s, strlen(s)); strncpy(node->value.ptr, s, strlen(s));
((u_char*)node->value.ptr)[strlen(s)] = '\0'; ((uint8_t*)node->value.ptr)[strlen(s)] = '\0';
return node; return node;
} }

View File

@ -74,7 +74,7 @@
int finsh_node_init(void); int finsh_node_init(void);
struct finsh_node* finsh_node_allocate(u_char type); struct finsh_node* finsh_node_allocate(uint8_t type);
struct finsh_node* finsh_node_new_id(char* id); struct finsh_node* finsh_node_new_id(char* id);
struct finsh_node* finsh_node_new_char(char c); struct finsh_node* finsh_node_new_char(char c);
struct finsh_node* finsh_node_new_int(int i); struct finsh_node* finsh_node_new_int(int i);

View File

@ -61,7 +61,7 @@ static struct finsh_node* proc_postfix_expr(struct finsh_parser* self);
static struct finsh_node* proc_primary_expr(struct finsh_parser* self); static struct finsh_node* proc_primary_expr(struct finsh_parser* self);
static struct finsh_node* proc_param_list(struct finsh_parser* self); static struct finsh_node* proc_param_list(struct finsh_parser* self);
static struct finsh_node* proc_expr_statement(struct finsh_parser* self); static struct finsh_node* proc_expr_statement(struct finsh_parser* self);
static struct finsh_node* make_sys_node(u_char type, struct finsh_node* node1, static struct finsh_node* make_sys_node(uint8_t type, struct finsh_node* node1,
struct finsh_node* node2); struct finsh_node* node2);
/* check token */ /* check token */
@ -894,7 +894,7 @@ node1__
\ \
node2 node2
*/ */
static struct finsh_node* make_sys_node(u_char type, struct finsh_node* node1, struct finsh_node* node2) static struct finsh_node* make_sys_node(uint8_t type, struct finsh_node* node1, struct finsh_node* node2)
{ {
struct finsh_node* node; struct finsh_node* node;
@ -913,7 +913,7 @@ static struct finsh_node* make_sys_node(u_char type, struct finsh_node* node1, s
/* /*
start -> statement_expr | decl_variable start -> statement_expr | decl_variable
*/ */
void finsh_parser_run(struct finsh_parser* self, const u_char* string) void finsh_parser_run(struct finsh_parser* self, const uint8_t* string)
{ {
enum finsh_token_type token; enum finsh_token_type token;
struct finsh_node *node; struct finsh_node *node;
@ -921,7 +921,7 @@ void finsh_parser_run(struct finsh_parser* self, const u_char* string)
node = NULL; node = NULL;
/* init parser */ /* init parser */
self->parser_string = (u_char*)string; self->parser_string = (uint8_t*)string;
/* init token */ /* init token */
finsh_token_init(&(self->token), self->parser_string); finsh_token_init(&(self->token), self->parser_string);

View File

@ -32,6 +32,6 @@
#include <finsh.h> #include <finsh.h>
int finsh_parser_init(struct finsh_parser* self); int finsh_parser_init(struct finsh_parser* self);
void finsh_parser_run(struct finsh_parser* self, const u_char* string); void finsh_parser_run(struct finsh_parser* self, const uint8_t* string);
#endif #endif

View File

@ -66,12 +66,12 @@ static long token_spec_number(char* string, int length, int b);
static void token_run(struct finsh_token* self); static void token_run(struct finsh_token* self);
static int token_match_name(struct finsh_token* self, const char* str); static int token_match_name(struct finsh_token* self, const char* str);
static void token_proc_number(struct finsh_token* self); static void token_proc_number(struct finsh_token* self);
static u_char* token_proc_string(struct finsh_token* self); static uint8_t* token_proc_string(struct finsh_token* self);
static void token_trim_space(struct finsh_token* self); static void token_trim_space(struct finsh_token* self);
static char token_proc_char(struct finsh_token* self); static char token_proc_char(struct finsh_token* self);
static int token_proc_escape(struct finsh_token* self); static int token_proc_escape(struct finsh_token* self);
void finsh_token_init(struct finsh_token* self, u_char* line) void finsh_token_init(struct finsh_token* self, uint8_t* line)
{ {
memset(self, 0, sizeof(struct finsh_token)); memset(self, 0, sizeof(struct finsh_token));
@ -86,12 +86,12 @@ enum finsh_token_type finsh_token_token(struct finsh_token* self)
return (enum finsh_token_type)self->current_token; return (enum finsh_token_type)self->current_token;
} }
void finsh_token_get_token(struct finsh_token* self, u_char* token) void finsh_token_get_token(struct finsh_token* self, uint8_t* token)
{ {
strncpy((char*)token, (char*)self->string, FINSH_NAME_MAX); strncpy((char*)token, (char*)self->string, FINSH_NAME_MAX);
} }
int token_get_string(struct finsh_token* self, u_char* str) int token_get_string(struct finsh_token* self, uint8_t* str)
{ {
unsigned char *p=str; unsigned char *p=str;
char ch; char ch;
@ -382,9 +382,9 @@ static char token_proc_char(struct finsh_token* self)
return ch; return ch;
} }
static u_char* token_proc_string(struct finsh_token* self) static uint8_t* token_proc_string(struct finsh_token* self)
{ {
u_char* p; uint8_t* p;
for ( p = &self->string[0]; p - &(self->string[0]) < FINSH_STRING_MAX; ) for ( p = &self->string[0]; p - &(self->string[0]) < FINSH_STRING_MAX; )
{ {

View File

@ -74,9 +74,9 @@ enum finsh_token_type
#define finsh_token_position(self) (self)->position #define finsh_token_position(self) (self)->position
#define finsh_token_replay(self) (self)->replay = 1 #define finsh_token_replay(self) (self)->replay = 1
void finsh_token_init(struct finsh_token* self, u_char* script); void finsh_token_init(struct finsh_token* self, uint8_t* script);
enum finsh_token_type finsh_token_token(struct finsh_token* self); enum finsh_token_type finsh_token_token(struct finsh_token* self);
void finsh_token_get_token(struct finsh_token* self, u_char* token); void finsh_token_get_token(struct finsh_token* self, uint8_t* token);
#endif #endif

View File

@ -104,7 +104,7 @@ struct finsh_var* finsh_var_lookup(const char* name)
} }
#ifdef RT_USING_HEAP #ifdef RT_USING_HEAP
void finsh_sysvar_append(const char* name, u_char type, void* var_addr) void finsh_sysvar_append(const char* name, uint8_t type, void* var_addr)
{ {
/* create a sysvar */ /* create a sysvar */
struct finsh_sysvar_item* item; struct finsh_sysvar_item* item;

View File

@ -39,7 +39,7 @@ struct finsh_var
{ {
char name[FINSH_NAME_MAX + 1]; /* the name of variable */ char name[FINSH_NAME_MAX + 1]; /* the name of variable */
u_char type; /* the type of variable */ uint8_t type; /* the type of variable */
/* variable value */ /* variable value */
union { union {

View File

@ -35,10 +35,10 @@
/* stack */ /* stack */
union finsh_value finsh_vm_stack[FINSH_STACK_MAX]; union finsh_value finsh_vm_stack[FINSH_STACK_MAX];
/* text segment */ /* text segment */
u_char text_segment[FINSH_TEXT_MAX]; uint8_t text_segment[FINSH_TEXT_MAX];
union finsh_value* finsh_sp; /* stack pointer */ union finsh_value* finsh_sp; /* stack pointer */
u_char* finsh_pc; /* PC */ uint8_t* finsh_pc; /* PC */
/* syscall list, for dynamic system call register */ /* syscall list, for dynamic system call register */
struct finsh_syscall_item* global_syscall_list = NULL; struct finsh_syscall_item* global_syscall_list = NULL;
@ -46,7 +46,7 @@ struct finsh_syscall_item* global_syscall_list = NULL;
// #define FINSH_VM_DISASSEMBLE // #define FINSH_VM_DISASSEMBLE
void finsh_vm_run() void finsh_vm_run()
{ {
u_char op; uint8_t op;
/* if you want to disassemble the byte code, please define FINSH_VM_DISASSEMBLE */ /* if you want to disassemble the byte code, please define FINSH_VM_DISASSEMBLE */
#ifdef FINSH_VM_DISASSEMBLE #ifdef FINSH_VM_DISASSEMBLE
@ -148,7 +148,7 @@ struct finsh_syscall* finsh_syscall_lookup(const char* name)
#ifdef FINSH_VM_DISASSEMBLE #ifdef FINSH_VM_DISASSEMBLE
void finsh_disassemble() void finsh_disassemble()
{ {
u_char *pc, op; uint8_t *pc, op;
pc = &text_segment[0]; pc = &text_segment[0];
while (*pc != 0) while (*pc != 0)

View File

@ -41,12 +41,12 @@ union finsh_value {
}; };
extern union finsh_value* finsh_sp; /* stack pointer */ extern union finsh_value* finsh_sp; /* stack pointer */
extern u_char* finsh_pc; /* PC */ extern uint8_t* finsh_pc; /* PC */
/* stack */ /* stack */
extern union finsh_value finsh_vm_stack[FINSH_STACK_MAX]; extern union finsh_value finsh_vm_stack[FINSH_STACK_MAX];
/* text segment */ /* text segment */
extern u_char text_segment[FINSH_TEXT_MAX]; extern uint8_t text_segment[FINSH_TEXT_MAX];
void finsh_vm_run(void); void finsh_vm_run(void);
//void finsh_disassemble(void); //void finsh_disassemble(void);

View File

@ -82,6 +82,23 @@ const char *finsh_get_prompt()
} }
#endif #endif
static char finsh_getchar(void)
{
RT_ASSERT(shell != RT_NULL);
#ifdef RT_USING_DFS
return getchar();
#else
char ch;
while (rt_device_read(shell->device, -1, &ch, 1) != 1)
rt_sem_take(&shell->rx_sem, RT_WAITING_FOREVER);
return ch;
#endif
}
#ifndef RT_USING_DFS
static rt_err_t finsh_rx_ind(rt_device_t dev, rt_size_t size) static rt_err_t finsh_rx_ind(rt_device_t dev, rt_size_t size)
{ {
RT_ASSERT(shell != RT_NULL); RT_ASSERT(shell != RT_NULL);
@ -145,6 +162,7 @@ const char *finsh_get_device()
RT_ASSERT(shell != RT_NULL); RT_ASSERT(shell != RT_NULL);
return shell->device->parent.name; return shell->device->parent.name;
} }
#endif
/** /**
* @ingroup finsh * @ingroup finsh
@ -219,15 +237,14 @@ static void finsh_wait_auth(void)
while (1) while (1)
{ {
rt_kprintf("Password for finsh: "); rt_kprintf("Password for login: ");
while (!input_finish) while (!input_finish)
{ {
/* wait receive */ while (1)
if (rt_sem_take(&shell->rx_sem, RT_WAITING_FOREVER) != RT_EOK) continue;
/* read one character from device */
while (rt_device_read(shell->device, 0, &ch, 1) == 1)
{ {
/* read one character from device */
ch = finsh_getchar();
if (ch >= ' ' && ch <= '~' && cur_pos < FINSH_PASSWORD_MAX) if (ch >= ' ' && ch <= '~' && cur_pos < FINSH_PASSWORD_MAX)
{ {
/* change the printable characters to '*' */ /* change the printable characters to '*' */
@ -258,8 +275,6 @@ static void finsh_wait_auth(void)
cur_pos = 0; cur_pos = 0;
input_finish = RT_FALSE; input_finish = RT_FALSE;
rt_memset(password, '\0', FINSH_PASSWORD_MAX); rt_memset(password, '\0', FINSH_PASSWORD_MAX);
/* read all last dirty data */
while (rt_device_read(shell->device, 0, &ch, 1) == 1);
} }
} }
} }
@ -397,18 +412,17 @@ void finsh_thread_entry(void *parameter)
finsh_init(&shell->parser); finsh_init(&shell->parser);
#endif #endif
#ifndef RT_USING_DFS
/* set console device as shell device */ /* set console device as shell device */
if (shell->device == RT_NULL) if (shell->device == RT_NULL)
{ {
#ifdef RT_USING_CONSOLE rt_device_t console = rt_console_get_device();
shell->device = rt_console_get_device(); if (console)
RT_ASSERT(shell->device); {
rt_device_set_rx_indicate(shell->device, finsh_rx_ind); finsh_set_device(console->parent.name);
rt_device_open(shell->device, (RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_STREAM | RT_DEVICE_FLAG_INT_RX)); }
#else
RT_ASSERT(shell->device);
#endif
} }
#endif
#ifdef FINSH_USING_AUTH #ifdef FINSH_USING_AUTH
/* set the default password when the password isn't setting */ /* set the default password when the password isn't setting */
@ -427,235 +441,219 @@ void finsh_thread_entry(void *parameter)
while (1) while (1)
{ {
/* wait receive */ ch = finsh_getchar();
if (rt_sem_take(&shell->rx_sem, RT_WAITING_FOREVER) != RT_EOK) continue;
/* read one character from device */ /*
while (rt_device_read(shell->device, 0, &ch, 1) == 1) * handle control key
* up key : 0x1b 0x5b 0x41
* down key: 0x1b 0x5b 0x42
* right key:0x1b 0x5b 0x43
* left key: 0x1b 0x5b 0x44
*/
if (ch == 0x1b)
{ {
/* shell->stat = WAIT_SPEC_KEY;
* handle control key continue;
* up key : 0x1b 0x5b 0x41 }
* down key: 0x1b 0x5b 0x42 else if (shell->stat == WAIT_SPEC_KEY)
* right key:0x1b 0x5b 0x43 {
* left key: 0x1b 0x5b 0x44 if (ch == 0x5b)
*/
if (ch == 0x1b)
{ {
shell->stat = WAIT_SPEC_KEY; shell->stat = WAIT_FUNC_KEY;
continue; continue;
} }
else if (shell->stat == WAIT_SPEC_KEY)
{
if (ch == 0x5b)
{
shell->stat = WAIT_FUNC_KEY;
continue;
}
shell->stat = WAIT_NORMAL; shell->stat = WAIT_NORMAL;
} }
else if (shell->stat == WAIT_FUNC_KEY) else if (shell->stat == WAIT_FUNC_KEY)
{ {
shell->stat = WAIT_NORMAL; shell->stat = WAIT_NORMAL;
if (ch == 0x41) /* up key */ if (ch == 0x41) /* up key */
{ {
#ifdef FINSH_USING_HISTORY #ifdef FINSH_USING_HISTORY
/* prev history */ /* prev history */
if (shell->current_history > 0) if (shell->current_history > 0)
shell->current_history --; shell->current_history --;
else else
{
shell->current_history = 0;
continue;
}
/* copy the history command */
memcpy(shell->line, &shell->cmd_history[shell->current_history][0],
FINSH_CMD_SIZE);
shell->line_curpos = shell->line_position = strlen(shell->line);
shell_handle_history(shell);
#endif
continue;
}
else if (ch == 0x42) /* down key */
{ {
#ifdef FINSH_USING_HISTORY shell->current_history = 0;
/* next history */
if (shell->current_history < shell->history_count - 1)
shell->current_history ++;
else
{
/* set to the end of history */
if (shell->history_count != 0)
shell->current_history = shell->history_count - 1;
else
continue;
}
memcpy(shell->line, &shell->cmd_history[shell->current_history][0],
FINSH_CMD_SIZE);
shell->line_curpos = shell->line_position = strlen(shell->line);
shell_handle_history(shell);
#endif
continue;
}
else if (ch == 0x44) /* left key */
{
if (shell->line_curpos)
{
rt_kprintf("\b");
shell->line_curpos --;
}
continue;
}
else if (ch == 0x43) /* right key */
{
if (shell->line_curpos < shell->line_position)
{
rt_kprintf("%c", shell->line[shell->line_curpos]);
shell->line_curpos ++;
}
continue; continue;
} }
} /* copy the history command */
memcpy(shell->line, &shell->cmd_history[shell->current_history][0],
/* handle CR key */ FINSH_CMD_SIZE);
if (ch == '\r')
{
char next;
if (rt_device_read(shell->device, 0, &next, 1) == 1)
{
if (next == '\0') ch = '\r'; /* linux telnet will issue '\0' */
else ch = next;
}
else ch = '\r';
}
/* handle tab key */
else if (ch == '\t')
{
int i;
/* move the cursor to the beginning of line */
for (i = 0; i < shell->line_curpos; i++)
rt_kprintf("\b");
/* auto complete */
shell_auto_complete(&shell->line[0]);
/* re-calculate position */
shell->line_curpos = shell->line_position = strlen(shell->line); shell->line_curpos = shell->line_position = strlen(shell->line);
shell_handle_history(shell);
#endif
continue; continue;
} }
/* handle backspace key */ else if (ch == 0x42) /* down key */
else if (ch == 0x7f || ch == 0x08)
{
/* note that shell->line_curpos >= 0 */
if (shell->line_curpos == 0)
continue;
shell->line_position--;
shell->line_curpos--;
if (shell->line_position > shell->line_curpos)
{
int i;
rt_memmove(&shell->line[shell->line_curpos],
&shell->line[shell->line_curpos + 1],
shell->line_position - shell->line_curpos);
shell->line[shell->line_position] = 0;
rt_kprintf("\b%s \b", &shell->line[shell->line_curpos]);
/* move the cursor to the origin position */
for (i = shell->line_curpos; i <= shell->line_position; i++)
rt_kprintf("\b");
}
else
{
rt_kprintf("\b \b");
shell->line[shell->line_position] = 0;
}
continue;
}
/* handle end of line, break */
if (ch == '\r' || ch == '\n')
{ {
#ifdef FINSH_USING_HISTORY #ifdef FINSH_USING_HISTORY
shell_push_history(shell); /* next history */
#endif if (shell->current_history < shell->history_count - 1)
shell->current_history ++;
#ifdef FINSH_USING_MSH
if (msh_is_used() == RT_TRUE)
{
if (shell->echo_mode)
rt_kprintf("\n");
msh_exec(shell->line, shell->line_position);
}
else else
#endif
{ {
#ifndef FINSH_USING_MSH_ONLY /* set to the end of history */
/* add ';' and run the command line */ if (shell->history_count != 0)
shell->line[shell->line_position] = ';'; shell->current_history = shell->history_count - 1;
if (shell->line_position != 0) finsh_run_line(&shell->parser, shell->line);
else else
if (shell->echo_mode) rt_kprintf("\n"); continue;
#endif
} }
rt_kprintf(FINSH_PROMPT); memcpy(shell->line, &shell->cmd_history[shell->current_history][0],
memset(shell->line, 0, sizeof(shell->line)); FINSH_CMD_SIZE);
shell->line_curpos = shell->line_position = 0; shell->line_curpos = shell->line_position = strlen(shell->line);
break; shell_handle_history(shell);
#endif
continue;
} }
else if (ch == 0x44) /* left key */
{
if (shell->line_curpos)
{
rt_kprintf("\b");
shell->line_curpos --;
}
/* it's a large line, discard it */ continue;
if (shell->line_position >= FINSH_CMD_SIZE) }
shell->line_position = 0; else if (ch == 0x43) /* right key */
{
if (shell->line_curpos < shell->line_position)
{
rt_kprintf("%c", shell->line[shell->line_curpos]);
shell->line_curpos ++;
}
/* normal character */ continue;
if (shell->line_curpos < shell->line_position) }
}
/* handle CR key */
if (ch == '\0') continue;
/* handle tab key */
else if (ch == '\t')
{
int i;
/* move the cursor to the beginning of line */
for (i = 0; i < shell->line_curpos; i++)
rt_kprintf("\b");
/* auto complete */
shell_auto_complete(&shell->line[0]);
/* re-calculate position */
shell->line_curpos = shell->line_position = strlen(shell->line);
continue;
}
/* handle backspace key */
else if (ch == 0x7f || ch == 0x08)
{
/* note that shell->line_curpos >= 0 */
if (shell->line_curpos == 0)
continue;
shell->line_position--;
shell->line_curpos--;
if (shell->line_position > shell->line_curpos)
{ {
int i; int i;
rt_memmove(&shell->line[shell->line_curpos + 1], rt_memmove(&shell->line[shell->line_curpos],
&shell->line[shell->line_curpos], &shell->line[shell->line_curpos + 1],
shell->line_position - shell->line_curpos); shell->line_position - shell->line_curpos);
shell->line[shell->line_curpos] = ch; shell->line[shell->line_position] = 0;
if (shell->echo_mode)
rt_kprintf("%s", &shell->line[shell->line_curpos]);
/* move the cursor to new position */ rt_kprintf("\b%s \b", &shell->line[shell->line_curpos]);
for (i = shell->line_curpos; i < shell->line_position; i++)
/* move the cursor to the origin position */
for (i = shell->line_curpos; i <= shell->line_position; i++)
rt_kprintf("\b"); rt_kprintf("\b");
} }
else else
{ {
shell->line[shell->line_position] = ch; rt_kprintf("\b \b");
if (shell->echo_mode) shell->line[shell->line_position] = 0;
rt_kprintf("%c", ch);
} }
ch = 0; continue;
shell->line_position ++; }
shell->line_curpos++;
if (shell->line_position >= FINSH_CMD_SIZE) /* handle end of line, break */
if (ch == '\r' || ch == '\n')
{
#ifdef FINSH_USING_HISTORY
shell_push_history(shell);
#endif
#ifdef FINSH_USING_MSH
if (msh_is_used() == RT_TRUE)
{ {
/* clear command line */ if (shell->echo_mode)
shell->line_position = 0; rt_kprintf("\n");
shell->line_curpos = 0; msh_exec(shell->line, shell->line_position);
} }
} /* end of device read */ else
} #endif
{
#ifndef FINSH_USING_MSH_ONLY
/* add ';' and run the command line */
shell->line[shell->line_position] = ';';
if (shell->line_position != 0) finsh_run_line(&shell->parser, shell->line);
else
if (shell->echo_mode) rt_kprintf("\n");
#endif
}
rt_kprintf(FINSH_PROMPT);
memset(shell->line, 0, sizeof(shell->line));
shell->line_curpos = shell->line_position = 0;
continue;
}
/* it's a large line, discard it */
if (shell->line_position >= FINSH_CMD_SIZE)
shell->line_position = 0;
/* normal character */
if (shell->line_curpos < shell->line_position)
{
int i;
rt_memmove(&shell->line[shell->line_curpos + 1],
&shell->line[shell->line_curpos],
shell->line_position - shell->line_curpos);
shell->line[shell->line_curpos] = ch;
if (shell->echo_mode)
rt_kprintf("%s", &shell->line[shell->line_curpos]);
/* move the cursor to new position */
for (i = shell->line_curpos; i < shell->line_position; i++)
rt_kprintf("\b");
}
else
{
shell->line[shell->line_position] = ch;
if (shell->echo_mode)
rt_kprintf("%c", ch);
}
ch = 0;
shell->line_position ++;
shell->line_curpos++;
if (shell->line_position >= FINSH_CMD_SIZE)
{
/* clear command line */
shell->line_position = 0;
shell->line_curpos = 0;
}
} /* end of device read */
} }
void finsh_system_function_init(const void *begin, const void *end) void finsh_system_function_init(const void *begin, const void *end)
@ -779,5 +777,5 @@ int finsh_system_init(void)
rt_thread_startup(&finsh_thread); rt_thread_startup(&finsh_thread);
return 0; return 0;
} }
INIT_COMPONENT_EXPORT(finsh_system_init); INIT_APP_EXPORT(finsh_system_init);

View File

@ -33,15 +33,8 @@
#include <rtthread.h> #include <rtthread.h>
#include "finsh.h" #include "finsh.h"
/* For historical reasons, users don't define FINSH_USING_HISTORY in rtconfig.h #ifndef FINSH_USING_HISTORY
* but expect the history feature. So you sould define FINSH_USING_HISTORY to 0 #define FINSH_USING_HISTORY
* to disable it from the rtconfig.h. */
#ifdef FINSH_USING_HISTORY
# if FINSH_USING_HISTORY == 0
# undef FINSH_USING_HISTORY
# endif
#else
# define FINSH_USING_HISTORY
#endif #endif
#ifndef FINSH_THREAD_PRIORITY #ifndef FINSH_THREAD_PRIORITY
@ -109,7 +102,9 @@ struct finsh_shell
rt_uint8_t line_position; rt_uint8_t line_position;
rt_uint8_t line_curpos; rt_uint8_t line_curpos;
#ifndef RT_USING_DFS
rt_device_t device; rt_device_t device;
#endif
#ifdef FINSH_USING_AUTH #ifdef FINSH_USING_AUTH
char password[FINSH_PASSWORD_MAX]; char password[FINSH_PASSWORD_MAX];