Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A collection of proposed enhancements #28

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Microrl library is designed to help implement command line interface in small an
- Ctrl+A (like HOME)
- Ctrl+E (like END)
- Ctrl+H (like backspace)
- Ctrl+D (delete one character forward)
- Ctrl+B (like cursor arrow left)
- Ctrl+F (like cursor arrow right)
- Ctrl+P (like cursor arrow up)
Expand All @@ -30,6 +31,9 @@ Microrl library is designed to help implement command line interface in small an
** completion
- via completion callback

** quoting (optional)
- Use single or double quotes around a command argument that needs to include space characters.


3. SRC STRUCTURE

Expand Down
62 changes: 31 additions & 31 deletions examples/avr_misc/avr_misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void init (void)
//}

//*****************************************************************************
void print (const char * str)
void print (void * pThis, const char * str)
{
int i = 0;
while (str [i] != 0) {
Expand All @@ -67,24 +67,24 @@ char get_char (void)
}

//*****************************************************************************
void print_help (void)
void print_help (void * pThis, void)
{
print ("Use TAB key for completion\n\rCommand:\n\r");
print ("\tclear - clear screen\n\r");
print ("\tset_port port pin - set 1 port[pin] value, support only 'port_b' and 'port_d'\n\r");
print ("\tclear_port port pin - set 0 port[pin] value, support only 'port_b' and 'port_d'\n\r");
print (pThis, "Use TAB key for completion\n\rCommand:\n\r");
print (pThis, "\tclear - clear screen\n\r");
print (pThis, "\tset_port port pin - set 1 port[pin] value, support only 'port_b' and 'port_d'\n\r");
print (pThis, "\tclear_port port pin - set 0 port[pin] value, support only 'port_b' and 'port_d'\n\r");
}

//*****************************************************************************
void set_port_val (unsigned char * port, int pin, int val)
void set_port_val (void * pThis, unsigned char * port, int pin, int val)
{
if ((*port == PORTD) && (pin < 2) && (pin > 7)) {
print ("only 2..7 pin avialable for PORTD\n\r");
print (pThis, "only 2..7 pin avialable for PORTD\n\r");
return;
}

if ((*port == PORTB) && (pin > 5)) {
print ("only 0..5 pin avialable for PORTB\n\r");
print (pThis, "only 0..5 pin avialable for PORTB\n\r");
return;
}

Expand All @@ -98,21 +98,21 @@ void set_port_val (unsigned char * port, int pin, int val)
//*****************************************************************************
// execute callback for microrl library
// do what you want here, but don't write to argv!!! read only!!
int execute (int argc, const char * const * argv)
int execute (void * pThis, int argc, const char * const * argv)
{
int i = 0;
// just iterate through argv word and compare it with your commands
while (i < argc) {
if (strcmp (argv[i], _CMD_HELP) == 0) {
print ("microrl v");
print (MICRORL_LIB_VER);
print (" library AVR DEMO v");
print (_AVR_DEMO_VER);
print (pThis, "microrl v");
print (pThis, MICRORL_LIB_VER);
print (pThis, " library AVR DEMO v");
print (pThis, _AVR_DEMO_VER);
print("\n\r");
print_help (); // print help
print_help (pThis); // print help
} else if (strcmp (argv[i], _CMD_CLEAR) == 0) {
print ("\033[2J"); // ESC seq for clear entire screen
print ("\033[H"); // ESC seq for move cursor at left-top corner
print (pThis, "\033[2J"); // ESC seq for clear entire screen
print (pThis, "\033[H"); // ESC seq for move cursor at left-top corner
} else if ((strcmp (argv[i], _CMD_SET) == 0) ||
(strcmp (argv[i], _CMD_CLR) == 0)) {
if (++i < argc) {
Expand All @@ -124,29 +124,29 @@ int execute (int argc, const char * const * argv)
} else if (strcmp (argv[i], _SCMD_PB) == 0) {
port = (unsigned char *)&PORTB;
} else {
print ("only '");
print (_SCMD_PB);
print ("' and '");
print (_SCMD_PD);
print ("' support\n\r");
print (pThis, "only '");
print (pThis, _SCMD_PB);
print (pThis, "' and '");
print (pThis, _SCMD_PD);
print (pThis, "' support\n\r");
return 1;
}
if (++i < argc) {
pin = atoi (argv[i]);
set_port_val (port, pin, val);
set_port_val (pThis, port, pin, val);
return 0;
} else {
print ("specify pin number, use Tab\n\r");
print (pThis, "specify pin number, use Tab\n\r");
return 1;
}
} else {
print ("specify port, use Tab\n\r");
print (pThis, "specify port, use Tab\n\r");
return 1;
}
} else {
print ("command: '");
print ((char*)argv[i]);
print ("' Not found.\n\r");
print (pThis, "command: '");
print (pThis, (char*)argv[i]);
print (pThis, "' Not found.\n\r");
}
i++;
}
Expand All @@ -156,7 +156,7 @@ int execute (int argc, const char * const * argv)
#ifdef _USE_COMPLETE
//*****************************************************************************
// completion callback for microrl library
char ** complet (int argc, const char * const * argv)
char ** complet (void * pThis, int argc, const char * const * argv)
{
int j = 0;

Expand Down Expand Up @@ -196,7 +196,7 @@ char ** complet (int argc, const char * const * argv)
#endif

//*****************************************************************************
void sigint (void)
void sigint (void * pThis)
{
print ("^C catched!\n\r");
print (pThis, "^C catched!\n\r");
}
8 changes: 4 additions & 4 deletions examples/example_misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ for AVR, linux PC or ARM
void init (void);

// print to stream callback
void print (const char * str);
void print (void * pThis, const char * str);

// get_char from stream
char get_char (void);

// execute callback
int execute (int argc, const char * const * argv);
int execute (void * pThis, int argc, const char * const * argv);

// completion callback
char ** complet (int argc, const char * const * argv);
char ** complet (void * pThis, int argc, const char * const * argv);

// ctrl+c callback
void sigint (void);
void sigint (void * pThis, void);

#endif
64 changes: 32 additions & 32 deletions examples/unix_misc/unix_misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void init (void){

//*****************************************************************************
// print callback for microrl library
void print (const char * str)
void print (void * pThis, const char * str)
{
fprintf (stdout, "%s", str);
}
Expand Down Expand Up @@ -64,66 +64,66 @@ int val;


//*****************************************************************************
void print_help ()
void print_help (void * pThis)
{
print ("Use TAB key for completion\n\rCommand:\n\r");
print ("\tversion {microrl | demo} - print version of microrl lib or version of this demo src\n\r");
print ("\thelp - this message\n\r");
print ("\tclear - clear screen\n\r");
print ("\tlist - list all commands in tree\n\r");
print ("\tname [string] - print 'name' value if no 'string', set name value to 'string' if 'string' present\n\r");
print ("\tlisp - dummy command for demonstation auto-completion, while inputed 'l+<TAB>'\n\r");
print (pThis, "Use TAB key for completion\n\rCommand:\n\r");
print (pThis, "\tversion {microrl | demo} - print version of microrl lib or version of this demo src\n\r");
print (pThis, "\thelp - this message\n\r");
print (pThis, "\tclear - clear screen\n\r");
print (pThis, "\tlist - list all commands in tree\n\r");
print (pThis, "\tname [string] - print 'name' value if no 'string', set name value to 'string' if 'string' present\n\r");
print (pThis, "\tlisp - dummy command for demonstation auto-completion, while inputed 'l+<TAB>'\n\r");
}

//*****************************************************************************
// execute callback for microrl library
// do what you want here, but don't write to argv!!! read only!!
int execute (int argc, const char * const * argv)
int execute (void * pThis, int argc, const char * const * argv)
{
int i = 0;
// just iterate through argv word and compare it with your commands
while (i < argc) {
if (strcmp (argv[i], _CMD_HELP) == 0) {
print ("microrl library based shell v 1.0\n\r");
print_help (); // print help
print (pThis, "microrl library based shell v 1.0\n\r");
print_help (pThis); // print help
} else if (strcmp (argv[i], _CMD_NAME) == 0) {
if ((++i) < argc) { // if value preset
if (strlen (argv[i]) < _NAME_LEN) {
strcpy (name, argv[i]);
} else {
print ("name value too long!\n\r");
print (pThis, "name value too long!\n\r");
}
} else {
print (name);
print ("\n\r");
print (pThis, name);
print (pThis, "\n\r");
}
} else if (strcmp (argv[i], _CMD_VER) == 0) {
if (++i < argc) {
if (strcmp (argv[i], _SCMD_DEMO) == 0) {
print ("demo v 1.0\n\r");
print (pThis, "demo v 1.0\n\r");
} else if (strcmp (argv[i], _SCMD_MRL) == 0) {
print ("microrl v 1.2\n\r");
print (pThis, "microrl v 1.2\n\r");
} else {
print ((char*)argv[i]);
print (" wrong argument, see help\n\r");
print (pThis, (char*)argv[i]);
print (pThis, " wrong argument, see help\n\r");
}
} else {
print ("version needs 1 parametr, see help\n\r");
print (pThis, "version needs 1 parametr, see help\n\r");
}
} else if (strcmp (argv[i], _CMD_CLEAR) == 0) {
print ("\033[2J"); // ESC seq for clear entire screen
print ("\033[H"); // ESC seq for move cursor at left-top corner
print (pThis, "\033[2J"); // ESC seq for clear entire screen
print (pThis, "\033[H"); // ESC seq for move cursor at left-top corner
} else if (strcmp (argv[i], _CMD_LIST) == 0) {
print ("available command:\n");// print all command per line
print (pThis, "available command:\n");// print all command per line
for (int i = 0; i < _NUM_OF_CMD; i++) {
print ("\t");
print (keyworld[i]);
print ("\n\r");
print (pThis, "\t");
print (pThis, keyworld[i]);
print (pThis, "\n\r");
}
} else {
print ("command: '");
print ((char*)argv[i]);
print ("' Not found.\n\r");
print (pThis, "command: '");
print (pThis, (char*)argv[i]);
print (pThis, "' Not found.\n\r");
}
i++;
}
Expand All @@ -133,7 +133,7 @@ int execute (int argc, const char * const * argv)
#ifdef _USE_COMPLETE
//*****************************************************************************
// completion callback for microrl library
char ** complet (int argc, const char * const * argv)
char ** complet (void * pThis, int argc, const char * const * argv)
{
int j = 0;

Expand Down Expand Up @@ -172,7 +172,7 @@ char ** complet (int argc, const char * const * argv)
#endif

//*****************************************************************************
void sigint (void)
void sigint (void * pThis)
{
print ("^C catched!\n\r");
print (pThis, "^C catched!\n\r");
}
31 changes: 28 additions & 3 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ NULL to callback ptr and do not use it, but for memory saving tune,
if you are not going to use it - disable this define.*/
#define _USE_COMPLETE

/*Define it, if you want to allow quoting command arguments to include spaces.
Depends upon _QUOTED_TOKEN_NMB parameter */
#define _USE_QUOTING

/*
Quoted token number, define max number of tokens allowed to be quoted. If the
number of quoted tokens typed in the command line exceeds this value, then
prints message about it and the command line is not parsed and 'execute'
callback is not called.
Quoting protects whitespace, for example 2 quoted tokens:
"IRin> set wifi 'Home Net' 'this is a secret'" */
#define _QUOTED_TOKEN_NMB 2

/*Define it, if you wanna use history. It s work's like bash history, and
set stored value to cmdline, if UP and DOWN key pressed. Using history add
memory consuming, depends from _RING_HISTORY_LEN parametr */
Expand All @@ -54,19 +67,31 @@ but memory using more effective. We not prefer dinamic memory allocation for
small and embedded devices. Overhead is 2 char on each saved line*/
#define _RING_HISTORY_LEN 64

/*
Size of the buffer used for piecemeal printing of part or all of the command
line. Allocated on the stack. Must be at least 16. */
#define _PRINT_BUFFER_LEN 40

/*
Enable Handling terminal ESC sequence. If disabling, then cursor arrow, HOME, END will not work,
use Ctrl+A(B,F,P,N,A,E,H,K,U,C) see README, but decrease code memory.*/
#define _USE_ESC_SEQ

/*
Use snprintf from you standard complier library, but it gives some overhead.
If not defined, use my own u16int_to_str variant, it's save about 800 byte of code size
on AVR (avr-gcc build).
Use sprintf from you standard complier library, but it gives some overhead.
If not defined, use my own number conversion code, it's save about 800 byte of
code size on AVR (avr-gcc build).
Try to build with and without, and compare total code size for tune library.
*/
#define _USE_LIBC_STDIO

/*
Use a single carriage return character to move the cursor to the left margin
rather than moving left by a large number. This reduces the number of
characters sent to the terminal, but should be left undefined if the terminal
will also simulate a linefeed when it receives the carriage return. */
#define _USE_CARRIAGE_RETURN

/*
Enable 'interrupt signal' callback, if user press Ctrl+C */
#define _USE_CTLR_C
Expand Down
Loading