format shell_filter

set sh_filter as default, add test caces
This commit is contained in:
pikastech 2023-02-13 15:25:54 +08:00
parent 4f12ac1785
commit 1265906952
7 changed files with 158 additions and 153 deletions

View File

@ -11,7 +11,7 @@
"program": "${workspaceFolder}/build/test/pikascript_test",
// "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain",
"args": [
// "--gtest_filter=except.except_break"
"--gtest_filter=pikaMain.SHELL_filter*"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",

View File

@ -33,9 +33,9 @@
#include "PikaPlatform.h"
#include "dataArgs.h"
#include "dataMemory.h"
#include "dataQueue.h"
#include "dataString.h"
#include "dataStrs.h"
#include "dataQueue.h"
static volatile Arg* _help_modules_cmodule = NULL;
static volatile PIKA_BOOL in_root_obj = PIKA_FALSE;
@ -979,58 +979,42 @@ static void _putc_cmd(char KEY_POS, int pos) {
}
#if PIKA_SHELL_FILTER_ENABLE
static
enum shellCTRL _inner_do_obj_runChar( PikaObj* self,
char inputChar,
ShellConfig* shell);
typedef enum {
__FILTER_NO_RESULT,
__FILTER_NO_RESULT,
__FILTER_FAIL_DROP_ONE,
__FILTER_SUCCESS_GET_ALL_PEEKED,
__FILTER_SUCCESS_DROP_ALL_PEEKED
} FilterReturn;
PIKA_BOOL _filter_msg_hi_pika_handler( FilterItem *msg,
PikaObj* self,
ShellConfig* shell)
{
#if !(defined(__linux) || defined(_WIN32))
PIKA_BOOL _filter_msg_hi_pika_handler(FilterItem* msg,
PikaObj* self,
ShellConfig* shell) {
pika_platform_printf("Yes, I am here\r\n");
#endif
return PIKA_TRUE;
}
PIKA_BOOL _filter_msg_bye_pika_handler( FilterItem *msg,
PikaObj* self,
ShellConfig* shell)
{
#if !(defined(__linux) || defined(_WIN32))
PIKA_BOOL _filter_msg_bye_pika_handler(FilterItem* msg,
PikaObj* self,
ShellConfig* shell) {
pika_platform_printf("OK, see you\r\n");
#endif
return PIKA_TRUE;
}
#define __MSG_DECLARE
#include "__default_filter_msg_table.cfg"
static const FilterItem c_default_filter_messages[] = {
static const FilterItem g_default_filter_messages[] = {
#define __MSG_TABLE
#include "__default_filter_msg_table.cfg"
};
static FilterReturn _do_message_filter( PikaObj* self,
ShellConfig* shell,
FilterItem *msg,
uint_fast16_t count)
{
static FilterReturn _do_message_filter(PikaObj* self,
ShellConfig* shell,
FilterItem* msg,
uint_fast16_t count) {
pika_assert(NULL != msg);
pika_assert(count > 0);
ByteQueue *queue = &shell->filter_fifo.queue;
ByteQueue* queue = &shell->filter_fifo.queue;
FilterReturn result = __FILTER_FAIL_DROP_ONE;
do {
@ -1039,25 +1023,25 @@ static FilterReturn _do_message_filter( PikaObj* self,
/* this message should be ignored */
break;
}
if (NULL == msg->message){
if (NULL == msg->message) {
break;
}
uint_fast16_t message_size = msg->size;
if (!message_size) {
break;
}
byte_queue_reset_peek(queue);
byteQueue_resetPeek(queue);
/* do message comparison */
uint8_t *src = (uint8_t *)msg->message;
uint8_t* src = (uint8_t*)msg->message;
if (msg->is_case_insensitive) {
do {
uint8_t byte;
if (!byte_queue_peek_one(queue, &byte)) {
if (!byteQueue_peekOne(queue, &byte)) {
result = __FILTER_NO_RESULT;
break;
}
@ -1066,28 +1050,28 @@ static FilterReturn _do_message_filter( PikaObj* self,
if (letter >= 'A' && letter <= 'Z') {
letter += 'a' - 'A';
}
if (byte >= 'A' && byte <= 'Z') {
byte += 'a' - 'A';
}
if (letter != byte) {
break;
}
} while(--message_size);
} while (--message_size);
} else {
do {
uint8_t byte;
if (!byte_queue_peek_one(queue, &byte)) {
if (!byteQueue_peekOne(queue, &byte)) {
result = __FILTER_NO_RESULT;
break;
}
if (*src++ != byte) {
break;
}
} while(--message_size);
} while (--message_size);
}
if (0 == message_size) {
/* message match */
if (NULL != msg->handler) {
@ -1101,28 +1085,26 @@ static FilterReturn _do_message_filter( PikaObj* self,
}
return __FILTER_SUCCESS_DROP_ALL_PEEKED;
}
} while(0);
} while (0);
msg++;
} while(--count);
} while (--count);
return result;
}
#ifndef dimof
#define dimof(__array) (sizeof(__array) / sizeof(__array[0]))
#define dimof(__array) (sizeof(__array) / sizeof(__array[0]))
#endif
int16_t _do_stream_filter(PikaObj* self, ShellConfig* shell){
int16_t _do_stream_filter(PikaObj* self, ShellConfig* shell) {
ByteQueue* queue = &shell->filter_fifo.queue;
ByteQueue *queue = &shell->filter_fifo.queue;
FilterReturn result = _do_message_filter(self,
shell,
(FilterItem *)c_default_filter_messages,
dimof(c_default_filter_messages));
FilterReturn result =
_do_message_filter(self, shell, (FilterItem*)g_default_filter_messages,
dimof(g_default_filter_messages));
int16_t drop_count = 0;
switch (result) {
case __FILTER_NO_RESULT:
break;
@ -1130,18 +1112,16 @@ int16_t _do_stream_filter(PikaObj* self, ShellConfig* shell){
drop_count = 1;
break;
case __FILTER_SUCCESS_DROP_ALL_PEEKED:
byte_queue_drop_all_peeked(queue);
byteQueue_dropAllPeeked(queue);
return 0;
case __FILTER_SUCCESS_GET_ALL_PEEKED:
drop_count = byte_queue_get_peeked_number(queue);
drop_count = byteQueue_getPeekedNumber(queue);
return drop_count;
}
/* user registered message filter */
if (NULL != shell->messages && shell->message_count) {
result = _do_message_filter(self,
shell,
shell->messages,
result = _do_message_filter(self, shell, shell->messages,
shell->message_count);
switch (result) {
case __FILTER_NO_RESULT:
@ -1150,67 +1130,22 @@ int16_t _do_stream_filter(PikaObj* self, ShellConfig* shell){
drop_count = 1;
break;
case __FILTER_SUCCESS_DROP_ALL_PEEKED:
byte_queue_drop_all_peeked(&shell->filter_fifo.queue);
byteQueue_dropAllPeeked(&shell->filter_fifo.queue);
return 0;
case __FILTER_SUCCESS_GET_ALL_PEEKED:
drop_count = byte_queue_get_peeked_number(&shell->filter_fifo.queue);
drop_count =
byteQueue_getPeekedNumber(&shell->filter_fifo.queue);
return drop_count;
}
}
return drop_count;
}
PIKA_WEAK
enum shellCTRL _do_obj_runChar(PikaObj* self,
char inputChar,
ShellConfig* shell) {
ByteQueue *queue = &(shell->filter_fifo.queue);
/* validation */
if (NULL == queue->buffer) {
/* need initialize first */
byte_queue_init(queue,
&shell->filter_fifo.buffer,
sizeof(shell->filter_fifo.buffer),
PIKA_FALSE);
}
PIKA_BOOL result = byte_queue_write_one(queue, inputChar);
pika_assert(result != PIKA_FALSE);
int16_t byte_count;
do {
if (0 == byte_queue_peek_available_count(queue)) {
break;
}
byte_count = _do_stream_filter(self, shell);
int16_t n = byte_count;
while(n--) {
PIKA_BOOL result = byte_queue_read_one(queue, (uint8_t *)&inputChar);
pika_assert(result != PIKA_FALSE);
if (SHELL_CTRL_EXIT == _inner_do_obj_runChar(self, inputChar, shell)) {
return SHELL_CTRL_EXIT;
}
}
} while(byte_count);
return SHELL_CTRL_CONTINUE;
}
static
enum shellCTRL _inner_do_obj_runChar( PikaObj* self,
char inputChar,
ShellConfig* shell) {
#else
enum shellCTRL _do_obj_runChar(PikaObj* self,
char inputChar,
ShellConfig* shell) {
#endif
enum shellCTRL _inner_do_obj_runChar(PikaObj* self,
char inputChar,
ShellConfig* shell) {
char* input_line = NULL;
enum shellCTRL ctrl = SHELL_CTRL_CONTINUE;
#if !(defined(__linux) || defined(_WIN32))
@ -1365,6 +1300,48 @@ exit:
return ctrl;
}
PIKA_WEAK
enum shellCTRL _do_obj_runChar(PikaObj* self,
char inputChar,
ShellConfig* shell) {
#if PIKA_SHELL_FILTER_ENABLE
ByteQueue* queue = &(shell->filter_fifo.queue);
/* validation */
if (NULL == queue->buffer) {
/* need initialize first */
byteQueue_init(queue, &shell->filter_fifo.buffer,
sizeof(shell->filter_fifo.buffer), PIKA_FALSE);
}
PIKA_BOOL result = byteQueue_writeOne(queue, inputChar);
pika_assert(result != PIKA_FALSE);
int16_t byte_count;
do {
if (0 == byteQueue_peekAvailableCount(queue)) {
break;
}
byte_count = _do_stream_filter(self, shell);
int16_t n = byte_count;
while (n--) {
PIKA_BOOL result = byteQueue_readOne(queue, (uint8_t*)&inputChar);
pika_assert(result != PIKA_FALSE);
if (SHELL_CTRL_EXIT ==
_inner_do_obj_runChar(self, inputChar, shell)) {
return SHELL_CTRL_EXIT;
}
}
} while (byte_count);
return SHELL_CTRL_CONTINUE;
#else
return _inner_do_obj_runChar(self, inputChar, shell);
#endif
}
enum shellCTRL obj_runChar(PikaObj* self, char inputChar) {
ShellConfig* shell = args_getStruct(self->list, "@shcfg");
if (NULL == shell) {

View File

@ -44,8 +44,8 @@
* )
*/
add_filter_msg(hi_pika, "Hi Pika")
add_filter_msg(bye_pika, "bye pika", .is_case_insensitive = PIKA_TRUE)
add_filter_msg(hi_pika, "###Hi Pika")
add_filter_msg(bye_pika, "###bye pika", .is_case_insensitive = PIKA_TRUE)
/* add your own message item here with syntax:
*

View File

@ -25,7 +25,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#define __DATA_QUEUE_CLASS_IMPLEMENT__
#define __DATA_QUEUE_CLASS_IMPLEMENT__
#include "dataQueue.h"
#include "PikaPlatform.h"
#include "dataArgs.h"
@ -96,12 +96,10 @@ char* queue_popStr(Queue* queue) {
return arg_getStr(__queue_popArg_noRmoveArg(queue));
}
ByteQueue *byte_queue_init( ByteQueue *queue,
void *buffer,
uint_fast16_t size,
PIKA_BOOL is_queue_full)
{
ByteQueue* byteQueue_init(ByteQueue* queue,
void* buffer,
uint_fast16_t size,
PIKA_BOOL is_queue_full) {
pika_assert(NULL != queue);
pika_assert(NULL != buffer);
pika_assert(size > 0);
@ -118,8 +116,7 @@ ByteQueue *byte_queue_init( ByteQueue *queue,
return queue;
}
PIKA_BOOL byte_queue_read_one(ByteQueue *queue, uint8_t *byte_ptr)
{
PIKA_BOOL byteQueue_readOne(ByteQueue* queue, uint8_t* byte_ptr) {
pika_assert(NULL != queue);
uint8_t byte;
PIKA_BOOL result = PIKA_FALSE;
@ -145,14 +142,13 @@ PIKA_BOOL byte_queue_read_one(ByteQueue *queue, uint8_t *byte_ptr)
*byte_ptr = byte;
}
result = PIKA_TRUE;
} while(0);
} while (0);
/* ------------------atomicity sensitive end ---------------- */
return result;
}
PIKA_BOOL byte_queue_peek_one(ByteQueue *queue, uint8_t *byte_ptr)
{
PIKA_BOOL byteQueue_peekOne(ByteQueue* queue, uint8_t* byte_ptr) {
pika_assert(NULL != queue);
uint8_t byte;
PIKA_BOOL result = PIKA_FALSE;
@ -174,14 +170,13 @@ PIKA_BOOL byte_queue_peek_one(ByteQueue *queue, uint8_t *byte_ptr)
*byte_ptr = byte;
}
result = PIKA_TRUE;
} while(0);
} while (0);
/* ------------------atomicity sensitive end ---------------- */
return result;
}
void byte_queue_reset_peek(ByteQueue *queue)
{
void byteQueue_resetPeek(ByteQueue* queue) {
pika_assert(NULL != queue);
/* ------------------atomicity sensitive start---------------- */
queue->peek_count = queue->count;
@ -189,19 +184,15 @@ void byte_queue_reset_peek(ByteQueue *queue)
/* ------------------atomicity sensitive end ---------------- */
}
uint_fast16_t byte_queue_get_peeked_number(ByteQueue *queue)
{
uint_fast16_t byteQueue_getPeekedNumber(ByteQueue* queue) {
return queue->count - queue->peek_count;
}
uint_fast16_t byte_queue_peek_available_count(ByteQueue *queue)
{
uint_fast16_t byteQueue_peekAvailableCount(ByteQueue* queue) {
return queue->peek_count;
}
void byte_queue_drop_all_peeked(ByteQueue *queue)
{
void byteQueue_dropAllPeeked(ByteQueue* queue) {
pika_assert(NULL != queue);
/* ------------------atomicity sensitive start---------------- */
queue->count = queue->peek_count;
@ -209,9 +200,7 @@ void byte_queue_drop_all_peeked(ByteQueue *queue)
/* ------------------atomicity sensitive end ---------------- */
}
PIKA_BOOL byte_queue_write_one(ByteQueue *queue, uint8_t byte)
{
PIKA_BOOL byteQueue_writeOne(ByteQueue* queue, uint8_t byte) {
pika_assert(NULL != queue);
PIKA_BOOL result = PIKA_FALSE;
@ -230,7 +219,7 @@ PIKA_BOOL byte_queue_write_one(ByteQueue *queue, uint8_t byte)
}
result = PIKA_TRUE;
} while(0);
} while (0);
/* ------------------atomicity sensitive end ---------------- */
return result;

View File

@ -71,16 +71,16 @@ Arg* queue_popArg_notDeinitArg(Queue* queue);
int32_t queue_deinit_stack(Queue* queue);
void queue_init(Queue* queue);
ByteQueue *byte_queue_init( ByteQueue *queue,
ByteQueue *byteQueue_init( ByteQueue *queue,
void *buffer,
uint_fast16_t size,
PIKA_BOOL is_queue_full);
PIKA_BOOL byte_queue_read_one(ByteQueue *queue, uint8_t *byte_ptr);
PIKA_BOOL byte_queue_peek_one(ByteQueue *queue, uint8_t *byte_ptr);
void byte_queue_reset_peek(ByteQueue *queue);
void byte_queue_drop_all_peeked(ByteQueue *queue);
uint_fast16_t byte_queue_get_peeked_number(ByteQueue *queue);
uint_fast16_t byte_queue_peek_available_count(ByteQueue *queue);
PIKA_BOOL byte_queue_write_one(ByteQueue *queue, uint8_t byte);
PIKA_BOOL byteQueue_readOne(ByteQueue *queue, uint8_t *byte_ptr);
PIKA_BOOL byteQueue_peekOne(ByteQueue *queue, uint8_t *byte_ptr);
void byteQueue_resetPeek(ByteQueue *queue);
void byteQueue_dropAllPeeked(ByteQueue *queue);
uint_fast16_t byteQueue_getPeekedNumber(ByteQueue *queue);
uint_fast16_t byteQueue_peekAvailableCount(ByteQueue *queue);
PIKA_BOOL byteQueue_writeOne(ByteQueue *queue, uint8_t byte);
#endif

View File

@ -65,11 +65,14 @@
#define PIKA_SYNTAX_LEVEL PIKA_SYNTAX_LEVEL_MINIMAL
#endif
#ifndef PIKA_STRING_UTF8_ENABLE
#define PIKA_STRING_UTF8_ENABLE 0
#endif
#ifndef PIKA_SHELL_FILTER_ENABLE
#define PIKA_SHELL_FILTER_ENABLE 0
#endif
#endif
/* default optimize */
@ -349,7 +352,7 @@
#endif
#ifndef PIKA_SHELL_FILTER_ENABLE
#define PIKA_SHELL_FILTER_ENABLE 0
#define PIKA_SHELL_FILTER_ENABLE 1
#endif
#ifndef PIKA_SHELL_FILTER_FIFO_SIZE

View File

@ -2832,6 +2832,42 @@ TEST(pikaMain, REPL_key_left_del) {
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(pikaMain, SHELL_filter_hi_pika) {
char lines[] = {"###Hi Pika"};
/* init */
pikaMemInfo.heapUsedMax = 0;
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
/* run */
__platform_printf("BEGIN\r\n");
for (size_t i = 0; i < strGetSize(lines); i++) {
obj_runChar(pikaMain, lines[i]);
}
/* collect */
/* assert */
EXPECT_STREQ(log_buff[0], "Yes, I am here\r\n");
/* deinit */
obj_deinit(pikaMain);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(pikaMain, SHELL_filter_bye_pika) {
char lines[] = {"###bye pika"};
/* init */
pikaMemInfo.heapUsedMax = 0;
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
/* run */
__platform_printf("BEGIN\r\n");
for (size_t i = 0; i < strGetSize(lines); i++) {
obj_runChar(pikaMain, lines[i]);
}
/* collect */
/* assert */
EXPECT_STREQ(log_buff[0], "OK, see you\r\n");
/* deinit */
obj_deinit(pikaMain);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(pikaMain, obj_setNone) {
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
obj_setNone(pikaMain, "n");