### Loading APM Modules at Runtime in Slash Source: https://context7.com/spaceinventor/slash/llms.txt This C code provides an example of how to dynamically load Slash Application Modules (APMs) at runtime using `dlopen`. The `load_apm_module` function takes a module path, loads the shared library, and then initializes the APM commands using `slash_init_apm`. The `main` function shows how to create a Slash instance, load multiple APMs, and then enter the command loop. ```c #include #include #include int load_apm_module(const char *module_path) { void *handle; /* Load the shared library */ handle = dlopen(module_path, RTLD_NOW | RTLD_GLOBAL); if (!handle) { fprintf(stderr, "Failed to load APM: %s\n", dlerror()); return -1; } /* Initialize APM commands */ slash_init_apm(handle); printf("Loaded APM: %s\n", module_path); return 0; } /* Example usage */ int main(void) { struct slash *slash = slash_create(128, 4096); /* Load APM modules */ load_apm_module("./my_apm.so"); load_apm_module("./another_apm.so"); /* APM commands are now available */ slash_loop(slash); slash_destroy(slash); return 0; } ``` -------------------------------- ### Defining Commands in Slash Application Modules (APMs) Source: https://context7.com/spaceinventor/slash/llms.txt This C code demonstrates how to define commands within a shared library (APM) for dynamic loading in Slash. It uses `SLASH_SECTION_INIT` to name the APM and `slash_sec_command` to register new commands specific to that module. The example shows two simple commands, `apm_test` and `apm_info`. ```c /* my_apm.c - Compile as shared library */ #include #include /* Initialize APM section with unique name */ SLASH_SECTION_INIT(my_apm) /* Define commands using section-specific macro */ static int cmd_apm_test(struct slash *slash) { printf("APM command executed\n"); return SLASH_SUCCESS; } /* Use slash_sec_command with section name */ slash_sec_command(my_apm, apm_test, cmd_apm_test, NULL, "Command from APM module"); static int cmd_apm_info(struct slash *slash) { printf("APM Version: 1.0\n"); return SLASH_SUCCESS; } slash_sec_command(my_apm, apm_info, cmd_apm_info, NULL, "Show APM information"); /* Compile with: gcc -shared -fPIC -o my_apm.so my_apm.c -lslash */ ``` -------------------------------- ### Initialize Slash Section for APM Source: https://github.com/spaceinventor/slash/blob/master/README.md This macro initializes the necessary start and stop variables for a separate command section within an APM. It also declares a function to retrieve pointers to these start and stop commands, which are then used to add the APM's commands to the main slash command list. ```c #define SLASH_SECTION_INIT(name) ``` -------------------------------- ### Initialize Slash Context C Source: https://context7.com/spaceinventor/slash/llms.txt Creates a new Slash context, enabling line editing and command history. Requires including the 'slash/slash.h' header. Returns a pointer to the Slash context or NULL on failure. Clean up with 'slash_destroy'. ```c #include #include #include #define LINE_SIZE 128 #define HISTORY_SIZE 4096 int main(int argc, char **argv) { struct slash *slash; /* Create slash context with line buffer and history */ slash = slash_create(LINE_SIZE, HISTORY_SIZE); if (!slash) { fprintf(stderr, "Failed to initialize slash\n"); return EXIT_FAILURE; } /* Start the interactive command loop */ slash_loop(slash); /* Clean up resources */ slash_destroy(slash); return EXIT_SUCCESS; } ``` -------------------------------- ### Iterating Through Registered Commands in C Source: https://context7.com/spaceinventor/slash/llms.txt Demonstrates how to iterate through all registered commands in the Slash framework at runtime using `slash_list_iterator` and `slash_list_iterate`. It prints the name and description of each available command. It also shows how to find a specific command by name using `slash_list_find_name`. ```c #include #include static int cmd_list_commands(struct slash *slash) { struct slash_command *cmd; slash_list_iterator iter = {}; int count = 0; printf("Available commands: "); printf("% -20s %s ", "Command", "Description"); printf("% -20s %s ", "-------", "-----------"); /* Iterate through all registered commands */ while ((cmd = slash_list_iterate(&iter)) != NULL) { printf("% -20s %s ", cmd->name, cmd->help ? cmd->help : "(no description)"); count++; } printf("\nTotal: %d commands ", count); return SLASH_SUCCESS; } /* Find specific command */ void find_and_execute(struct slash *slash, const char *cmd_name) { struct slash_command *cmd = slash_list_find_name(cmd_name); if (cmd && cmd->func) { printf("Found command: %s ", cmd->name); /* Can call cmd->func(slash) to execute */ } } slash_command(list, cmd_list_commands, NULL, "List all available commands"); ``` -------------------------------- ### Parse Command Arguments C Source: https://context7.com/spaceinventor/slash/llms.txt Demonstrates parsing command-line arguments and options within a Slash command. Uses 'slash_getopt' for option parsing, similar to standard C getopt. It handles flags like '-v' for verbose mode and options with arguments like '-c count'. ```c #include #include static int cmd_args(struct slash *slash) { int verbose = 0; int count = 1; int opt; /* Parse options using slash_getopt */ while ((opt = slash_getopt(slash, "vc:")) != -1) { switch (opt) { case 'v': verbose = 1; break; case 'c': count = atoi(slash->optarg); break; default: return SLASH_EUSAGE; } } /* Remaining arguments start at slash->argv[slash->optind] */ if (verbose) { printf("Verbose mode enabled\n"); } printf("Processing %d items\n", count); /* Print all remaining arguments */ for (int i = slash->optind; i < slash->argc; i++) { printf("Argument %d: '%s'\n", i, slash->argv[i]); } return SLASH_SUCCESS; } slash_command(args, cmd_args, "[-v] [-c count] [arg ... ]", "Example of argument parsing"); ``` -------------------------------- ### Implementing Custom Command Hooks in Slash Source: https://context7.com/spaceinventor/slash/llms.txt This C code illustrates how to implement various hooks in Slash to intercept and modify command execution. It includes pre-execution logging, post-execution reporting, command line processing (like alias expansion), and file execution hooks. These hooks allow for advanced control over the command interpreter's behavior. ```c #include #include #include /* Hook called before command execution */ void slash_on_execute_hook(const char *line) { time_t now = time(NULL); printf("[%s] Executing: %s\n", ctime(&now), line); } /* Hook called after command execution */ void slash_on_execute_post_hook(const char *line, struct slash_command *command) { printf("Completed: %s (command: %s)\n", line, command->name); } /* Process and modify command line before execution */ char *slash_process_cmd_line_hook(const char *line) { /* Example: expand aliases */ if (strncmp(line, "ll", 2) == 0) { return strdup("list long"); /* Will be freed automatically */ } return NULL; /* Use original line */ } /* Hook for file execution */ void slash_on_run_pre_hook(const char *filename, void **ctx_for_post) { printf("Running script: %s\n", filename); *ctx_for_post = strdup(filename); /* Pass context to post hook */ } void slash_on_run_post_hook(const char *filename, void *ctx) { printf("Finished script: %s\n", (char *)ctx); free(ctx); } ``` -------------------------------- ### Execute Commands Programmatically C Source: https://context7.com/spaceinventor/slash/llms.txt Shows how to execute Slash commands programmatically from within an application. The 'slash_execute' function takes the Slash context and a command string as input. It returns a status code indicating success or failure, allowing for handling of various error conditions. ```c #include #include void run_startup_commands(struct slash *slash) { int ret; /* Execute a command string */ ret = slash_execute(slash, "help"); if (ret != SLASH_SUCCESS) { printf("Command failed with code: %d\n", ret); } /* Execute command with arguments */ ret = slash_execute(slash, "test argument1 argument2"); /* Handle different return codes */ switch (ret) { case SLASH_SUCCESS: printf("Command succeeded\n"); break; case SLASH_EUSAGE: printf("Usage error\n"); break; case SLASH_EINVAL: printf("Invalid argument\n"); break; case SLASH_ENOENT: printf("Command not found\n"); break; } } ``` -------------------------------- ### Working with Command History in Slash Source: https://context7.com/spaceinventor/slash/llms.txt This C code demonstrates how to programmatically manage and access command history within the Slash library. It includes a `cmd_show_history` function to iterate through and display the history, and an `add_to_history` function to manually add commands to the history. A Slash command `show_history` is also registered to make this functionality accessible. ```c #include #include static int cmd_show_history(struct slash *slash) { char *p = slash->history_head; int line_num = 1; /* Iterate through history */ while (p != slash->history_tail) { if (*p) { printf("%4d ", line_num++); } slash_putchar(slash, *p ? *p : '\n'); p = slash_history_increment(slash, p); } return SLASH_SUCCESS; } void add_to_history(struct slash *slash, const char *command) { /* Manually add entry to history */ slash_history_add(slash, (char *)command); } slash_command(show_history, cmd_show_history, NULL, "Display command history"); ``` -------------------------------- ### Define Simple Commands C Source: https://context7.com/spaceinventor/slash/llms.txt Defines simple commands that are registered with the Slash command registry using macros. Commands are C functions that return an integer status code. The 'slash_command' macro registers a command with its name, function, argument description, and help text. ```c #include #include static int cmd_hello(struct slash *slash) { printf("Hello, World!\n"); return SLASH_SUCCESS; } /* Register command: name, function, args description, help text */ slash_command(hello, cmd_hello, NULL, "Print a greeting message"); static int cmd_test(struct slash *slash) { if (slash->argc < 2) return SLASH_EUSAGE; /* Show usage message */ printf("Test command received: %s\n", slash->argv[1]); return SLASH_SUCCESS; } slash_command(test, cmd_test, "", "A simple test command\n\n" "Requires a single argument which must be present"); ``` -------------------------------- ### Custom Prompt Implementation in C Source: https://context7.com/spaceinventor/slash/llms.txt Overrides the default prompt in the Slash framework with custom formatting, including the current working directory and a custom prompt character. It utilizes ANSI escape codes for coloring and displays the prompt in cyan with a gray '%' character. Dependencies include ``, ``, and ``. ```c #include #include #include /* Override weak symbol to provide custom prompt */ int slash_prompt(struct slash *slash) { char cwd[256]; const char *prompt_color = "\033[96m"; /* Cyan */ const char *reset_color = "\033[0m"; const char *prompt_char = "\033[90m%\033[0m "; /* Gray % */ /* Get current directory */ if (getcwd(cwd, sizeof(cwd)) != NULL) { int len = slash_printf(slash, "%s%s %s", prompt_color, cwd, prompt_char); return len; /* Return visible character count */ } int len = slash_printf(slash, "%sslash %s", prompt_color, prompt_char); return len; } ``` -------------------------------- ### Reading Input Lines with Slash Source: https://context7.com/spaceinventor/slash/llms.txt This C code snippet demonstrates how to read and process user input line by line using the slash_readline function. It includes support for terminal configuration, line editing, and command history. The loop continues until the user enters 'quit'. ```c #include #include #include void interactive_prompt(struct slash *slash) { char *line; /* Configure terminal for raw mode */ slash_acquire_std_in_out(slash); /* Read a line with history and editing */ while ((line = slash_readline(slash)) != NULL) { /* Check for empty line */ if (strlen(line) == 0) { continue; } /* Process the line */ printf("You entered: %s\n", line); /* Line is automatically added to history */ /* Exit on "quit" */ if (strcmp(line, "quit") == 0) { break; } } /* Restore terminal to normal mode */ slash_release_std_in_out(slash); } ``` -------------------------------- ### Create Subcommands C Source: https://context7.com/spaceinventor/slash/llms.txt Organizes commands into a hierarchical structure using subcommands. The 'slash_command_sub' macro registers a subcommand under a parent group. This allows for command grouping like 'network status' or 'network reset'. ```c #include #include static int cmd_network_status(struct slash *slash) { printf("Network Status: Connected\n"); return SLASH_SUCCESS; } /* Create subcommand: group, name, function, args, help */ slash_command_sub(network, status, cmd_network_status, NULL, "Show network connection status"); static int cmd_network_reset(struct slash *slash) { printf("Resetting network interface...\n"); return SLASH_SUCCESS; } slash_command_sub(network, reset, cmd_network_reset, NULL, "Reset network interface"); /* Usage: "network status" or "network reset" */ ``` -------------------------------- ### Define Standard Slash Command Source: https://github.com/spaceinventor/slash/blob/master/README.md This macro defines a standard slash command. The created command struct is placed in the default 'slash' ELF section, making it compatible with existing applications like csh without requiring changes to command definitions. ```c slash_command(name, func, args, help) ``` -------------------------------- ### Interruptible Wait Operations in C Source: https://context7.com/spaceinventor/slash/llms.txt Implements long-running operations within the Slash framework that can be interrupted by user input (pressing Enter). It uses `slash_wait_interruptible` to pause for a specified duration while monitoring for user interruptions or signals. Returns `SLASH_SUCCESS` on completion or cancellation, and `SLASH_EBREAK` if a signal is received. ```c #include #include static int cmd_long_operation(struct slash *slash) { int iterations = 10; printf("Starting long operation (press Enter to cancel)... "); for (int i = 0; i < iterations; i++) { printf("Progress: %d/%d ", i + 1, iterations); /* Wait 1 second, but allow user to interrupt */ int ret = slash_wait_interruptible(slash, 1000); if (ret == -EINTR) { printf("Operation cancelled by user "); return SLASH_SUCCESS; } /* Check for signal */ if (slash->signal) { printf("Received signal, aborting "); return SLASH_EBREAK; } } printf("Operation completed "); return SLASH_SUCCESS; } slash_command(long_op, cmd_long_operation, NULL, "Demonstrate interruptible operation"); ``` -------------------------------- ### Define Slash Command in APM Section Source: https://github.com/spaceinventor/slash/blob/master/README.md This macro defines a slash command specifically for an APM, allowing it to be placed in a unique, APM-defined ELF section. This is crucial for APMs to avoid section name collisions with the main application and ensures proper integration into the command list. ```c slash_sec_command(section, name, func, args, help) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.