MCPcopy Create free account
hub / github.com/OpenIPC/firmware / handle_cmd

Function handle_cmd

general/package/legacy/lshell/src/lshell.c:222–294  ·  view source on GitHub ↗

* @brief Parses and runs a string containing a cmd and optional parameters * * @param cmd_str * @return pid_t pid of process running command */

Source from the content-addressed store, hash-verified

220 * @return pid_t pid of process running command
221 */
222pid_t handle_cmd(char *cmd_str)
223{
224 pid_t res = 0;
225 char *redirect = NULL;
226 if (get_redirect_file(cmd_str, &redirect) != 0)
227 {
228 print_err();
229 return 1;
230 }
231
232 char *cmd = NULL;
233 cmd = strtok(cmd_str, " ");
234 if (cmd == NULL)
235 {
236 return res;
237 }
238
239 char **args = (char **)malloc(sizeof(char *) * 2);
240 args[0] = strdup(cmd);
241 size_t args_n = 1;
242
243 char *token = NULL;
244 token = strtok(NULL, " ");
245 while (token != NULL)
246 {
247 args[args_n] = (char *)malloc(sizeof(char) * strlen(token) + 1);
248 strcpy(args[args_n++], token);
249
250 args = (char **)realloc(args, (args_n + 1) * sizeof(char *));
251 token = strtok(NULL, " ");
252 }
253
254 // null terminate the arg array
255 args = realloc(args, (args_n + 1) * sizeof(char *));
256 args[args_n++] = NULL;
257
258 if (strcmp(cmd, "cd") == 0) // Check if commands are built-in commands
259 {
260
261 if (args_n != 3) // If there is less 2 args (program name[0] and directory[1]) cannot run
262 {
263 print_err();
264 res = 0;
265 }
266 else
267 {
268 if (chdir(args[1]) == -1)
269 {
270 print_err();
271 }
272 res = 0;
273 }
274 }
275 else if (strcmp(cmd, "path") == 0)
276 {
277 clear_paths();
278
279 for (size_t i = 1; i < args_n - 1; i++) // -1 because arg array is null terminated

Callers 1

handle_lineFunction · 0.85

Calls 6

get_redirect_fileFunction · 0.85
print_errFunction · 0.85
clear_pathsFunction · 0.85
add_pathFunction · 0.85
exec_cmdFunction · 0.85
free_char_arrFunction · 0.85

Tested by

no test coverage detected