Skip to content

File syn_cli.h

FileList > cli > syn_cli.h

Go to the source code of this file

Command-line interpreter for embedded systems. More...

  • #include "../common/syn_defs.h"
  • #include "../port/syn_port_serial.h"
  • #include <stdbool.h>
  • #include <stddef.h>

Classes

Type Name
struct SYN_CLI
CLI instance — command table, line buffer, I/O, and history.
struct SYN_CLI_Command
Command descriptor.

Public Types

Type Name
typedef int(* SYN_CLI_Handler
Command handler function.

Public Functions

Type Name
void syn_cli_init (SYN_CLI * cli, const SYN_CLI_Command * commands, size_t cmd_count, const char * prompt)
Initialize a CLI instance.
void syn_cli_print_prompt (SYN_CLI * cli)
void syn_cli_printf (SYN_CLI * cli, const char * fmt, ...)
Print formatted output through the CLI's output function.
void syn_cli_process_char (SYN_CLI * cli, char ch)
Process a single received character.
void syn_cli_process_line (SYN_CLI * cli, const char * line)
Process a complete null-terminated line.
void syn_cli_refresh_prompt (SYN_CLI * cli)
Refresh / redraw the prompt and active draft line.
void syn_cli_set_echo (SYN_CLI * cli, bool echo)
Enable or disable local echo.
void syn_cli_set_errlog (struct SYN_ErrLog * errlog)
Set errlog instance for the errors built-in command.
void syn_cli_set_scheduler (struct SYN_Sched * sched)
Set scheduler instance for the tasks built-in command.

Macros

Type Name
define SYN_CLI_CMD_ERRORS 1
Enable built-in 'errors' command.
define SYN_CLI_CMD_TASKS 1
Enable built-in 'tasks' command.
define SYN_CLI_CMD_UPTIME 1
Enable built-in 'uptime' command.
define SYN_CLI_CMD_VERSION 1
Register built-in diagnostic commands.
define SYN_CLI_HISTORY_DEPTH 0
define SYN_CLI_LINE_BUF_SIZE 128
define SYN_CLI_MAX_ARGS 16

Detailed Description

Provides a lightweight interactive shell over UART (or any byte stream). Commands are registered as a static table — no dynamic allocation. Input is processed one character at a time, making it easy to feed from a UART ISR or polled loop.

** **

  • Statically-registered command table (name + help + handler)
  • Automatic argc/argv parsing (whitespace-delimited, with quoted strings)
  • Built-in help command
  • Backspace and line editing
  • Configurable prompt
  • Echo control (for terminals that don't echo locally)
  • Command history (optional, configurable depth)

** **

// Define commands
static int cmd_led(int argc, char *argv[]);
static int cmd_reset(int argc, char *argv[]);

static const SYN_CLI_Command commands[] = {
    { "led",   "led <on|off>  — Control the LED",  cmd_led   },
    { "reset", "reset         — System reset",     cmd_reset },
};

// Initialize
static SYN_CLI cli;
syn_cli_init(&cli, commands, 2, my_putchar, "> ");

// In main loop or UART RX ISR:
syn_cli_process_char(&cli, received_byte);

Public Types Documentation

typedef SYN_CLI_Handler

Command handler function.

typedef int(* SYN_CLI_Handler) (int argc, char *argv[]);

Parameters:

  • argc Argument count (including the command name).
  • argv Argument vector. argv[0] is the command name.

Returns:

0 on success, or an application-defined error code.


Public Functions Documentation

function syn_cli_init

Initialize a CLI instance.

void syn_cli_init (
    SYN_CLI * cli,
    const SYN_CLI_Command * commands,
    size_t cmd_count,
    const char * prompt
) 

Output goes directly to syn_port_serial_write — no callback needed.

Parameters:

  • cli CLI instance to initialize.
  • commands Array of command descriptors.
  • cmd_count Number of commands.
  • prompt Prompt string (e.g., "> " or "syntropic> "). Stored by pointer, not copied.

function syn_cli_print_prompt

void syn_cli_print_prompt (
    SYN_CLI * cli
) 

function syn_cli_printf

Print formatted output through the CLI's output function.

void syn_cli_printf (
    SYN_CLI * cli,
    const char * fmt,
    ...
) 

Automatically manages prompt restoration during asynchronous logging.

Parameters:

  • cli CLI instance.
  • fmt Format string.
  • ... Arguments.

function syn_cli_process_char

Process a single received character.

void syn_cli_process_char (
    SYN_CLI * cli,
    char ch
) 

Call this for each byte received from the input stream (UART RX ISR, polled read, etc.). The CLI handles line editing (backspace) and dispatches the command when a newline is received.

Parameters:

  • cli CLI instance.
  • ch Received character.

function syn_cli_process_line

Process a complete null-terminated line.

void syn_cli_process_line (
    SYN_CLI * cli,
    const char * line
) 

Alternative to process_char when you already have a full line (e.g., from a buffered read). Does not echo or handle editing.

Parameters:

  • cli CLI instance.
  • line Null-terminated command line.

function syn_cli_refresh_prompt

Refresh / redraw the prompt and active draft line.

void syn_cli_refresh_prompt (
    SYN_CLI * cli
) 

Useful after external serial output to restore prompt visual state.

Parameters:

  • cli CLI instance.

function syn_cli_set_echo

Enable or disable local echo.

void syn_cli_set_echo (
    SYN_CLI * cli,
    bool echo
) 

When enabled (default), the CLI echoes each received character back to the terminal. Disable if the terminal handles its own echo.

Parameters:

  • cli CLI instance.
  • echo true to enable echo, false to disable.

function syn_cli_set_errlog

Set errlog instance for the errors built-in command.

void syn_cli_set_errlog (
    struct SYN_ErrLog * errlog
) 

Parameters:

  • errlog Error log instance (or NULL to detach).

function syn_cli_set_scheduler

Set scheduler instance for the tasks built-in command.

void syn_cli_set_scheduler (
    struct SYN_Sched * sched
) 

Parameters:

  • sched Scheduler instance (or NULL to detach).

Macro Definition Documentation

define SYN_CLI_CMD_ERRORS

Enable built-in 'errors' command.

#define SYN_CLI_CMD_ERRORS `1`


define SYN_CLI_CMD_TASKS

Enable built-in 'tasks' command.

#define SYN_CLI_CMD_TASKS `1`


define SYN_CLI_CMD_UPTIME

Enable built-in 'uptime' command.

#define SYN_CLI_CMD_UPTIME `1`


define SYN_CLI_CMD_VERSION

Register built-in diagnostic commands.

#define SYN_CLI_CMD_VERSION `1`

Adds framework-aware commands to the CLI. Each is individually guarded by a define (default: all enabled). The commands operate on the framework singletons you pass in.

Parameters:

  • cli CLI instance.

Built-in commands (enable/disable via defines): * version — print syn_version() info (SYN_CLI_CMD_VERSION, default 1) * uptime — print tick_ms uptime (SYN_CLI_CMD_UPTIME, default 1) * errors — dump errlog entries (SYN_CLI_CMD_ERRORS, default 1) * tasks — show scheduler task states (SYN_CLI_CMD_TASKS, default 1)


define SYN_CLI_HISTORY_DEPTH

#define SYN_CLI_HISTORY_DEPTH `0`

Command history depth (0 = disabled).


define SYN_CLI_LINE_BUF_SIZE

#define SYN_CLI_LINE_BUF_SIZE `128`

Maximum length of a single command line (including null terminator).


define SYN_CLI_MAX_ARGS

#define SYN_CLI_MAX_ARGS `16`

Maximum number of arguments (including the command name).



The documentation for this class was generated from the following file src/syntropic/cli/syn_cli.h