Command Line Interpreter (xwsh)

XWOS Command Line Interpreter module, providing an interactive Shell and command parsing functionality.

Overview

XWSH (XWOS Shell) is the command line interpreter module of XWOS, providing users with an interactive command line interface. It is located in the xwmd/cli/xwsh/ directory of the source code and belongs to the command line interface (cli) submodule of the XWOS middleware layer (xwmd).

The core functionality of XWSH includes:

  • Command Parsing: Splitting the user-input string into a command name and arguments
  • Command Execution: Finding and calling the corresponding command handler function
  • Line Editing: Providing history, auto-completion, and other features based on the CherryRL library
  • Extension Mechanism: Supporting users adding custom commands

Module Architecture

XWSH adopts a layered design. The relationships between components are as follows:

User Input
    ↓
[m] Readline Layer (CherryRL Integration)
    ↓
[c] Core Layer (Command Parsing and Execution)
    ↓
    ├── [c] Built-in Command Table
    ├── [c] External Command Table (User Extensions)
    └── [c] Command Lookup and Dispatch
        ↓
        [c] Command Handler Functions
  • Interface Layer (m): Provides module integration API (mi.h)
  • Core Layer (c): Command parsing, execution, management (core.h/core.c)
  • Readline Layer: Line editing functionality (readline.h/readline.c)
  • Command Layer: Built-in command implementation (cmd.c)

Features

1. Two Running Modes

  • Standalone Thread Mode: Creates a dedicated thread to run the Shell
  • Embedded Mode: Integrates the Shell loop into an existing thread

2. Command System

  • Built-in Commands: help, clear, rd (read memory), wr (write memory)
  • External Commands: Supports users dynamically adding custom commands
  • Duplicate Name Detection: Prevents command name conflicts

3. Line Editing Features

  • History: Supports command history browsing and retrieval
  • Auto-completion: Reserved completion interface
  • ANSI Escape: Color prompt support

4. Resource Footprint

  • Static Memory: No dynamic memory allocation, suitable for embedded environments
  • Fixed Buffers: Input buffer 128 bytes, maximum 16 arguments

Directory Navigation

  • Core: Command parsing algorithm, thread model, error handling
  • Command: Detailed built-in commands, external command adding methods
  • Extension: CherryRL library integration details, callback function descriptions
  • Integration: Code examples and best practices for both running modes
  • API Reference: Detailed descriptions of all public and internal APIs

Quick Start

Standalone Thread Mode

#include "xwmd/cli/xwsh/mi.h"

xwstk_t xwsh_stack[1024];
xwer_t rc = xwsh_start(xwsh_stack, sizeof(xwsh_stack));

Embedded Mode

#include "xwmd/cli/xwsh/mi.h"

xwsh_init();
while (1) {
    char buf[XWSH_MAXINPUT];
    xwsh_loop(buf);
}

Source Code Locations

Module Component Source File Description
Public Interface xwmd/cli/xwsh/mi.h
xwmd/cli/xwsh/mi.c
Module integration API
Core xwmd/cli/xwsh/core.h
xwmd/cli/xwsh/core.c
Command parsing and execution
Line Editing xwmd/cli/xwsh/readline.h
xwmd/cli/xwsh/readline.c
CherryRL integration
Built-in Commands xwmd/cli/xwsh/cmd.c Default command implementation

Note: The XWSH module requires standard input/output (stdin/stdout), typically implemented via serial port or virtual console.


Core

XWSH Core: Command parsing algorithm, thread model, error handling, and internal state management.

Command System

XWSH Command System: Built-in command details, external command adding methods, and duplicate name checking mechanism.

Extension

XWSH Extension Mechanism: CherryRL library integration details, callback function descriptions, and history implementation.

Integration Guide

XWSH Integration Guide: Detailed comparison of both running modes, code examples, and best practices.

API Reference

XWSH module API Reference: Detailed descriptions of all public and internal functions.