6 #include <sys/module.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
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");
27 ("unload <library>... - unloads the libraries from the environment\n");
28 printf("list - prints the currently loaded modules\n");
32 * print_prompt - Display the command prompt.
34 static void print_prompt(void)
36 printf("\nelflink> ");
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
)
49 fgets(cmd
, size
, stdin
);
52 nl
= strchr(cmd
, '\n');
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 *));
71 crt_arg
= strtok(NULL
, COMMAND_DELIM
);
72 if (crt_arg
!= NULL
&& strlen(crt_arg
) > 0) {
73 cmd_line
[argc
] = crt_arg
;
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");
85 result
= spawnv(cmd_line
[0], cmd_line
);
87 printf("Spawn returned %d\n", result
);
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
)
104 while ((crt_lib
= strtok(NULL
, COMMAND_DELIM
)) != NULL
) {
105 if (strlen(crt_lib
) > 0) {
107 result
= load_library(crt_lib
);
109 result
= unload_library(crt_lib
);
112 printf("Library '%s' %sloaded successfully.\n", crt_lib
,
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
);
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
)
154 cmd_name
= strtok(cmd
, COMMAND_DELIM
);
156 if (strcmp(cmd_name
, "exit") == 0) {
157 printf("Goodbye!\n");
159 } else if (strcmp(cmd_name
, "help") == 0) {
161 } else if (strcmp(cmd_name
, "load") == 0) {
163 } else if (strcmp(cmd_name
, "spawn") == 0) {
165 } else if (strcmp(cmd_name
, "unload") == 0) {
167 } else if (strcmp(cmd_name
, "list") == 0) {
170 printf("Unknown command. Type 'help' for a list of valid commands.\n");
177 * The entry point of 'test_com32' COM module.
179 int main(int argc
, char **argv
)
183 char command
[MAX_COMMAND_SIZE
] = { 0 };
185 // Open a standard r/w console
186 openconsole(&dev_stdcon_r
, &dev_stdcon_w
);
190 printf("Failed to initialize the execution environment.\n");
193 printf("Execution environment initialized successfully.\n");
196 printf("\nFor a list of available commands, type 'help'.\n");
200 read_command(command
, MAX_COMMAND_SIZE
);
201 done
= process_command(command
);