/* * An example plug-in, which implements the 'cd' command. */ #include #include #include #include #include #include "../esh.h" #include #include "../esh-sys-utils.h" static bool init_plugin(struct esh_shell *shell) { printf("Plugin 'cd' initialized...\n"); return true; } /* Implement chdir built-in. * Returns true if handled, false otherwise. */ static bool chdir_builtin(struct esh_command *cmd) { if (strcmp(cmd->argv[0], "m")) return false; if (cmd -> argv[1] == NULL || cmd -> argv[2] == NULL || cmd -> argv[3] == NULL) { printf("Not enough arguments to execute 'math'\n"); } char* operation = cmd -> argv[1]; int a = atoi(cmd -> argv[2]); int b = atoi(cmd -> argv[3]); int c; if (strcmp(operation, "add") == 0) { c = a + b; printf("%d + %d is equal to: %d\n", a, b, c); } else if (strcmp(operation, "subtract") == 0) { c = a - b; printf("%d - %d is equal to: %d\n", a, b, c); } else if (strcmp(operation, "multiply") == 0) { c = a * b; printf("%d * %d is equal to: %d\n", a, b, c); } else if (strcmp(operation, "divide") == 0) { c = a / b; printf("%d / %d is equal to: %d\n", a, b, c); } else { printf("Invalid mathematical operation\n"); } return true; } struct esh_plugin esh_module = { .rank = 1, .init = init_plugin, .process_builtin = chdir_builtin };