Adding upstream version 6.02~pre7+dfsg.
[syslinux-debian/hramrach.git] / com32 / elflink / test_com32.c
blob19089dbdfaff1123a0d085ccb9f5c12e1add469d
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <console.h>
4 #include <string.h>
6 #include <sys/module.h>
7 #include <sys/exec.h>
9 #define INFO_PRINT(fmt, args...) printf("[COM32] " fmt, ##args)
11 #define MAX_COMMAND_SIZE 80 // Maximum size of the cmd line
12 #define COMMAND_DELIM " \t\n" // Whitespace delimiters
13 #define MAX_COMMAND_ARGS (MAX_COMMAND_SIZE/2) // Maximum argument count for
14 // program execution
16 /**
17 * print_help - Display usage instructions on the screen.
19 static void print_help(void)
21 printf("List of available commands:\n");
22 printf("exit - exits the program\n");
23 printf("help - shows this message\n");
24 printf("load <library>... - loads the libraries into the environment\n");
25 printf("spawn <executable> <args> - launches an executable module\n");
26 printf
27 ("unload <library>... - unloads the libraries from the environment\n");
28 printf("list - prints the currently loaded modules\n");
31 /**
32 * print_prompt - Display the command prompt.
34 static void print_prompt(void)
36 printf("\nelflink> ");
39 /**
40 * read_command - Read a new command from the standard input.
41 * @cmd: the buffer to store the command
42 * @size: the maximum size of the string that can be stored in the buffer
44 * If the command is larger than the specified size, it is truncated.
46 static void read_command(char *cmd, int size)
48 char *nl = NULL;
49 fgets(cmd, size, stdin);
51 // Strip the newline
52 nl = strchr(cmd, '\n');
54 if (nl != NULL)
55 *nl = '\0';
58 /**
59 * process_spawn - Handles the execution of a 'spawn' command.
61 * The command line is in the internal buffer of strtok.
63 static void process_spawn(void)
65 // Compose the command line
66 char **cmd_line = malloc((MAX_COMMAND_ARGS + 1) * sizeof(char *));
67 int argc = 0, result;
68 char *crt_arg;
70 do {
71 crt_arg = strtok(NULL, COMMAND_DELIM);
72 if (crt_arg != NULL && strlen(crt_arg) > 0) {
73 cmd_line[argc] = crt_arg;
74 argc++;
75 } else {
76 break;
78 } while (argc < MAX_COMMAND_ARGS);
80 cmd_line[argc] = NULL;
82 if (cmd_line[0] == NULL) {
83 printf("You must specify an executable module.\n");
84 } else {
85 result = spawnv(cmd_line[0], cmd_line);
87 printf("Spawn returned %d\n", result);
90 free(cmd_line);
93 /**
94 * process_library - Handles the execution of the 'load' and 'unload' commands.
95 * @load: contains 1 if the libraries are to be loaded, 0 for unloading.
97 * The command line is in the internal buffer of strtok.
99 static void process_library(int load)
101 char *crt_lib;
102 int result;
104 while ((crt_lib = strtok(NULL, COMMAND_DELIM)) != NULL) {
105 if (strlen(crt_lib) > 0) {
106 if (load)
107 result = load_library(crt_lib);
108 else
109 result = unload_library(crt_lib);
111 if (result == 0) {
112 printf("Library '%s' %sloaded successfully.\n", crt_lib,
113 load ? "" : "un");
114 } else {
115 printf("Could not %sload library '%s': error %d\n",
116 load ? "" : "un", crt_lib, result);
123 * process_list - Handles the execution of the 'list' command.
126 static void process_list(void)
128 struct elf_module *module;
129 struct module_dep *crt_dep;
131 for_each_module(module) {
132 printf("%s (%dK, %s, %s) : ", module->name, module->module_size >> 10,
133 module->shallow ? "shallow" : "regular",
134 module->main_func == NULL ? "library" : "program");
136 list_for_each_entry(crt_dep, &module->required, list) {
137 printf("%s ", crt_dep->module->name);
140 printf("\n");
145 * process_command - Recognizes the requested command and executes it.
146 * @cmd: the command to be executed.
148 * Returns 1 if the command was 'exit', 0 otherwise.
150 static int process_command(char *cmd)
152 char *cmd_name;
154 cmd_name = strtok(cmd, COMMAND_DELIM);
156 if (strcmp(cmd_name, "exit") == 0) {
157 printf("Goodbye!\n");
158 return 1;
159 } else if (strcmp(cmd_name, "help") == 0) {
160 print_help();
161 } else if (strcmp(cmd_name, "load") == 0) {
162 process_library(1);
163 } else if (strcmp(cmd_name, "spawn") == 0) {
164 process_spawn();
165 } else if (strcmp(cmd_name, "unload") == 0) {
166 process_library(0);
167 } else if (strcmp(cmd_name, "list") == 0) {
168 process_list();
169 } else {
170 printf("Unknown command. Type 'help' for a list of valid commands.\n");
173 return 0;
177 * The entry point of 'test_com32' COM module.
179 int main(int argc, char **argv)
181 int done = 0;
182 int res;
183 char command[MAX_COMMAND_SIZE] = { 0 };
185 // Open a standard r/w console
186 openconsole(&dev_stdcon_r, &dev_stdcon_w);
188 res = exec_init();
189 if (res != 0) {
190 printf("Failed to initialize the execution environment.\n");
191 return res;
192 } else {
193 printf("Execution environment initialized successfully.\n");
196 printf("\nFor a list of available commands, type 'help'.\n");
198 do {
199 print_prompt();
200 read_command(command, MAX_COMMAND_SIZE);
201 done = process_command(command);
203 } while (!done);
205 exec_term();
207 return 0;