added busy command to the example + declared option parser options as const

This commit is contained in:
prozessorkern 2020-03-28 12:08:01 +01:00
parent d7962a54dc
commit 96cf0c8d65
5 changed files with 98 additions and 60 deletions

2
.vscode/launch.json vendored
View File

@ -9,7 +9,7 @@
"type": "cppdbg", "type": "cppdbg",
"request": "launch", "request": "launch",
"program": "${workspaceFolder}/output/example/example", "program": "${workspaceFolder}/output/example/example",
"args": ["/dev/pts/3"], "args": ["/dev/pts/4"],
"stopAtEntry": false, "stopAtEntry": false,
"cwd": "${workspaceFolder}", "cwd": "${workspaceFolder}",
"environment": [], "environment": [],

View File

@ -192,16 +192,16 @@ shellmatta_retCode_t shellmatta_read( shellmatta_handle_t handle,
uint32_t *length); uint32_t *length);
shellmatta_retCode_t shellmatta_opt( shellmatta_handle_t handle, shellmatta_retCode_t shellmatta_opt( shellmatta_handle_t handle,
char *optionString, const char *optionString,
char *option, char *option,
char **argument, char **argument,
uint32_t *argLen); uint32_t *argLen);
shellmatta_retCode_t shellmatta_opt_long( shellmatta_handle_t handle, shellmatta_retCode_t shellmatta_opt_long( shellmatta_handle_t handle,
shellmatta_opt_long_t *longOptions, const shellmatta_opt_long_t *longOptions,
char *option, char *option,
char **argument, char **argument,
uint32_t *argLen); uint32_t *argLen);
#ifndef SHELLMATTA_STRIP_PRINTF #ifndef SHELLMATTA_STRIP_PRINTF
shellmatta_retCode_t shellmatta_printf( shellmatta_handle_t handle, shellmatta_retCode_t shellmatta_printf( shellmatta_handle_t handle,

View File

@ -1,19 +1,23 @@
/* /*
* main.c * Copyright (c) 2019 Stefan Strobel <stefan.strobel@shimatta.net>
* *
* Created on: Jun 10, 2019 * This Source Code Form is subject to the terms of the Mozilla Public
* Author: stefan * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/ */
/**
* @file main.c
* @brief main module to demonstrate use of the shellmatta.
* @author Stefan Strobel <stefan.strobel@shimatta.net>
*/
#include "shellmatta.h" #include "shellmatta.h"
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <ncurses.h>
#include <stdbool.h> #include <stdbool.h>
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <termios.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
@ -22,24 +26,6 @@ static bool exitRequest = false;
int f; int f;
shellmatta_handle_t handle; shellmatta_handle_t handle;
void set_blocking (int fd, int should_block)
{
struct termios tty;
memset (&tty, 0, sizeof tty);
if (tcgetattr (fd, &tty) != 0)
{
printf ("error %d from tggetattr", errno);
return;
}
tty.c_cc[VMIN] = should_block ? 1 : 0;
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
if (tcsetattr (fd, TCSANOW, &tty) != 0)
printf ("error %d setting term attributes", errno);
}
static shellmatta_retCode_t doSomething(shellmatta_handle_t handle, const char *arguments, uint32_t length) static shellmatta_retCode_t doSomething(shellmatta_handle_t handle, const char *arguments, uint32_t length)
{ {
shellmatta_printf(handle, "%s - length: %u", arguments, length); shellmatta_printf(handle, "%s - length: %u", arguments, length);
@ -101,23 +87,47 @@ shellmatta_cmd_t emptyCommand = {"empty", NULL, NULL, NULL, empty, NULL};
static shellmatta_retCode_t reset(shellmatta_handle_t handle, const char *arguments, uint32_t length) static shellmatta_retCode_t reset(shellmatta_handle_t handle, const char *arguments, uint32_t length)
{ {
shellmatta_retCode_t ret;
(void)arguments; (void)arguments;
(void)length; (void)length;
char option;
char *argument;
uint32_t argLen;
bool printPrompt = false;
if(0 == strncmp(arguments, "prompt", length)) static const shellmatta_opt_long_t options[] =
{ {
shellmatta_resetShell(handle, true); {"prompt", 'p', SHELLMATTA_OPT_ARG_REQUIRED},
} {NULL, '\0', SHELLMATTA_OPT_ARG_NONE}
else };
ret = shellmatta_opt_long(handle, options, &option, &argument, &argLen);
while(SHELLMATTA_OK == ret)
{ {
shellmatta_resetShell(handle, false); switch(option)
{
case 'p':
if(NULL != argument)
{
if(0 == strncmp("true", argument, 4u))
{
printPrompt = true;
}
}
break;
default:
shellmatta_printf(handle, "Unknown option: %c\r\n", option);
break;
}
ret = shellmatta_opt_long(handle, options, &option, &argument, &argLen);
} }
shellmatta_resetShell(handle, printPrompt);
shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, true, '\r'); shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, true, '\r');
return SHELLMATTA_OK; return SHELLMATTA_OK;
} }
shellmatta_cmd_t resetCommand = {"reset", NULL, "resets the shellmatta instance", "reset [prompt]", reset, NULL}; shellmatta_cmd_t resetCommand = {"reset", NULL, "resets the shellmatta instance", "reset [--prompt true/false]", reset, NULL};
static shellmatta_retCode_t continuous(shellmatta_handle_t handle, const char *arguments, uint32_t length) static shellmatta_retCode_t continuous(shellmatta_handle_t handle, const char *arguments, uint32_t length)
{ {
@ -143,6 +153,28 @@ static shellmatta_retCode_t continuous(shellmatta_handle_t handle, const char *a
} }
shellmatta_cmd_t continuousCommand = {"continuous", "cont", "prints continously all input bytes", "continuous", continuous, NULL}; shellmatta_cmd_t continuousCommand = {"continuous", "cont", "prints continously all input bytes", "continuous", continuous, NULL};
static shellmatta_retCode_t busy(shellmatta_handle_t handle, const char *arguments, uint32_t length)
{
(void)arguments;
(void)length;
static uint32_t callCnt = 0u;
shellmatta_retCode_t ret = SHELLMATTA_BUSY;
if(callCnt < 10u)
{
callCnt ++;
shellmatta_printf(handle, "%s - length %u - callCnt %u\r\n", arguments, length, callCnt);
}
else
{
callCnt = 0u;
ret = SHELLMATTA_OK;
}
return ret;
}
shellmatta_cmd_t busyCommand = {"busy", NULL, NULL, NULL, busy, NULL};
shellmatta_retCode_t writeFct(const char* data, uint32_t length) shellmatta_retCode_t writeFct(const char* data, uint32_t length)
{ {
@ -171,8 +203,6 @@ int main(int argc, char **argv)
return f; return f;
} }
set_blocking (f, 1);
shellmatta_doInit( &instance, shellmatta_doInit( &instance,
&handle, &handle,
buffer, buffer,
@ -189,18 +219,26 @@ int main(int argc, char **argv)
shellmatta_addCmd(handle, &emptyCommand); shellmatta_addCmd(handle, &emptyCommand);
shellmatta_addCmd(handle, &resetCommand); shellmatta_addCmd(handle, &resetCommand);
shellmatta_addCmd(handle, &continuousCommand); shellmatta_addCmd(handle, &continuousCommand);
shellmatta_addCmd(handle, &busyCommand);
while(exitRequest == false) while(exitRequest == false)
{ {
char c; char c;
shellmatta_retCode_t ret;
int res = 0; int res = 0;
res = read (f, &c, 1); res = read (f, &c, 1);
fprintf(stdout, "0x%02x \n", c); fprintf(stdout, "0x%02x \n", c);
fflush(stdout); fflush(stdout);
shellmatta_processData(handle, &c, res); do
{
ret = shellmatta_processData(handle, &c, res);
if(SHELLMATTA_BUSY == ret)
{
sleep(1);
}
} while(SHELLMATTA_BUSY == ret);
} }
close(f); close(f);

View File

@ -124,7 +124,7 @@ static char peekNextHunk(shellmatta_instance_t *inst)
* #SHELLMATTA_ERROR - format error or option unknown * #SHELLMATTA_ERROR - format error or option unknown
*/ */
static shellmatta_retCode_t parseShortOpt( shellmatta_instance_t *inst, static shellmatta_retCode_t parseShortOpt( shellmatta_instance_t *inst,
char *optionString, const char *optionString,
char *option, char *option,
shellmatta_opt_argtype_t *argtype) shellmatta_opt_argtype_t *argtype)
{ {
@ -186,7 +186,7 @@ static shellmatta_retCode_t parseShortOpt( shellmatta_instance_t *inst,
* #SHELLMATTA_ERROR - format error or option unknown * #SHELLMATTA_ERROR - format error or option unknown
*/ */
static shellmatta_retCode_t parseLongOpt( shellmatta_instance_t *inst, static shellmatta_retCode_t parseLongOpt( shellmatta_instance_t *inst,
shellmatta_opt_long_t *longOptions, const shellmatta_opt_long_t *longOptions,
char *option, char *option,
shellmatta_opt_argtype_t *argtype) shellmatta_opt_argtype_t *argtype)
{ {
@ -253,12 +253,12 @@ static shellmatta_retCode_t parseLongOpt( shellmatta_instance_t *inst,
* @return errorcode #SHELLMATTA_OK - no error - keep on calling * @return errorcode #SHELLMATTA_OK - no error - keep on calling
* #SHELLMATTA_ERROR - error occured - e.g. argument missing * #SHELLMATTA_ERROR - error occured - e.g. argument missing
*/ */
static shellmatta_retCode_t shellmatta_opt_int( shellmatta_handle_t handle, static shellmatta_retCode_t shellmatta_opt_int( shellmatta_handle_t handle,
char *optionString, const char *optionString,
shellmatta_opt_long_t *longOptions, const shellmatta_opt_long_t *longOptions,
char *option, char *option,
char **argument, char **argument,
uint32_t *argLen) uint32_t *argLen)
{ {
shellmatta_retCode_t ret = SHELLMATTA_USE_FAULT; shellmatta_retCode_t ret = SHELLMATTA_USE_FAULT;
shellmatta_instance_t *inst = (shellmatta_instance_t*)handle; shellmatta_instance_t *inst = (shellmatta_instance_t*)handle;
@ -370,7 +370,7 @@ static shellmatta_retCode_t shellmatta_opt_int( shellmatta_handle_t handle,
* #SHELLMATTA_ERROR - error occured - e.g. argument missing * #SHELLMATTA_ERROR - error occured - e.g. argument missing
*/ */
shellmatta_retCode_t shellmatta_opt( shellmatta_handle_t handle, shellmatta_retCode_t shellmatta_opt( shellmatta_handle_t handle,
char *optionString, const char *optionString,
char *option, char *option,
char **argument, char **argument,
uint32_t *argLen) uint32_t *argLen)
@ -391,11 +391,11 @@ shellmatta_retCode_t shellmatta_opt( shellmatta_handle_t handle,
* @param[out] argument pointer to store the argument string to (can be NULL) * @param[out] argument pointer to store the argument string to (can be NULL)
* @param[out] argLen pointer to store the argument lengh to (can be NULL) * @param[out] argLen pointer to store the argument lengh to (can be NULL)
*/ */
shellmatta_retCode_t shellmatta_opt_long( shellmatta_handle_t handle, shellmatta_retCode_t shellmatta_opt_long( shellmatta_handle_t handle,
shellmatta_opt_long_t *longOptions, const shellmatta_opt_long_t *longOptions,
char *option, char *option,
char **argument, char **argument,
uint32_t *argLen) uint32_t *argLen)
{ {
return shellmatta_opt_int( handle, return shellmatta_opt_int( handle,
NULL, NULL,

View File

@ -23,16 +23,16 @@
#include <stdint.h> #include <stdint.h>
shellmatta_retCode_t shellmatta_opt( shellmatta_handle_t handle, shellmatta_retCode_t shellmatta_opt( shellmatta_handle_t handle,
char *optionString, const char *optionString,
char *option, char *option,
char **argument, char **argument,
uint32_t *argLen); uint32_t *argLen);
shellmatta_retCode_t shellmatta_opt_long( shellmatta_handle_t handle, shellmatta_retCode_t shellmatta_opt_long( shellmatta_handle_t handle,
shellmatta_opt_long_t *longOptions, const shellmatta_opt_long_t *longOptions,
char *option, char *option,
char **argument, char **argument,
uint32_t *argLen); uint32_t *argLen);
shellmatta_retCode_t shellmatta_opt_init( shellmatta_instance_t *inst, shellmatta_retCode_t shellmatta_opt_init( shellmatta_instance_t *inst,
uint32_t argStart); uint32_t argStart);