1.移除mr_msh_recv_char函数降低降低调用深度。

2.dselect新增-g获取自身描述符命令。
This commit is contained in:
MacRsh
2024-01-21 18:06:14 +08:00
parent 5276d02378
commit 27014a95d4
2 changed files with 32 additions and 28 deletions

View File

@@ -121,10 +121,7 @@ static void msh_insert_char(char c)
} }
/* Check whether the cursor is at the end of the buffer */ /* Check whether the cursor is at the end of the buffer */
if (msh.cursor == msh.bytes) if (msh.cursor != msh.bytes)
{
msh.buf[msh.bytes] = c;
} else
{ {
/* Insert the character */ /* Insert the character */
mr_msh_printf(MSH_INSERT_CHAR(1)); mr_msh_printf(MSH_INSERT_CHAR(1));
@@ -134,8 +131,8 @@ static void msh_insert_char(char c)
{ {
msh.buf[i + 1] = msh.buf[i]; msh.buf[i + 1] = msh.buf[i];
} }
msh.buf[msh.cursor] = c;
} }
msh.buf[msh.cursor] = c;
msh.cursor++; msh.cursor++;
msh.bytes++; msh.bytes++;
msh.buf[msh.bytes] = '\0'; msh.buf[msh.bytes] = '\0';
@@ -165,7 +162,7 @@ static int msh_parse_cmd(void)
/* Execute the command */ /* Execute the command */
for (const struct mr_msh_cmd *msh_cmd = ((&_mr_msh_cmd_start) + 1); msh_cmd < &_mr_msh_cmd_end; msh_cmd++) for (const struct mr_msh_cmd *msh_cmd = ((&_mr_msh_cmd_start) + 1); msh_cmd < &_mr_msh_cmd_end; msh_cmd++)
{ {
if (strcmp(msh_cmd->name, msh.buf) != 0) if (strncmp(msh_cmd->name, msh.buf, MR_CFG_MSH_NAME_MAX) != 0)
{ {
continue; continue;
} }
@@ -356,18 +353,6 @@ static void msh_parse_key(char c)
} }
} }
static void mr_msh_recv_char(char c)
{
/* Judgments are characters and keys */
if (MSH_IS_PRINTABLE(c) && (msh.key_bytes == 0))
{
msh_insert_char(c);
} else
{
msh_parse_key(c);
}
}
static int msh_cmd_help(int argc, void *argv) static int msh_cmd_help(int argc, void *argv)
{ {
for (const struct mr_msh_cmd *msh_cmd = ((&_mr_msh_cmd_start) + 1); msh_cmd < &_mr_msh_cmd_end; msh_cmd++) for (const struct mr_msh_cmd *msh_cmd = ((&_mr_msh_cmd_start) + 1); msh_cmd < &_mr_msh_cmd_end; msh_cmd++)
@@ -537,7 +522,14 @@ void mr_msh_handle(void)
char c; char c;
while (mr_msh_input(&c) > 0) while (mr_msh_input(&c) > 0)
{ {
mr_msh_recv_char(c); /* Judgments are characters and keys */
if (MSH_IS_PRINTABLE(c) && (msh.key_bytes == 0))
{
msh_insert_char(c);
} else
{
msh_parse_key(c);
}
} }
} }

View File

@@ -147,18 +147,30 @@ static int msh_cmd_dselect(int argc, void *argv)
goto usage; goto usage;
} }
/* Parse <desc (>=0)> */ /* Parse <-g|desc (>=0)> */
ret = sscanf(MR_MSH_GET_ARG(1), "%d", &desc); if (strncmp(MR_MSH_GET_ARG(1), "-g", 2) == 0)
if ((ret < 1) || (desc < 0))
{ {
goto usage; mr_msh_printf("%d\r\n", MSH_GET_DESC());
return MR_EOK;
} else
{
ret = sscanf(MR_MSH_GET_ARG(1), "%d", &desc);
if ((ret < 1) || (desc < 0))
{
goto usage;
}
/* Switch to the new descriptor */
ret = msh_update_path(desc);
if (ret < 0)
{
mr_msh_printf("error: %s\r\n", mr_strerror(ret));
}
return MR_EOK;
} }
/* Switch to the new descriptor */
return msh_update_path(desc);
usage: usage:
mr_msh_printf("usage: dselect <desc (>=0)>\r\n"); mr_msh_printf("usage: dselect <desc (>=0)|-g>\r\n");
return MR_EINVAL; return MR_EINVAL;
} }
@@ -669,7 +681,7 @@ static int msh_cmd_dread(int argc, void *argv)
} }
/* Read data */ /* Read data */
size = MR_BOUND(size *itemsz, 0, sizeof(buf)); size = MR_BOUND(size * itemsz, 0, sizeof(buf));
ret = (int)mr_dev_read(MSH_GET_DESC(), buf, size); ret = (int)mr_dev_read(MSH_GET_DESC(), buf, size);
if (ret < 0) if (ret < 0)
{ {