2026-01-06 09:48:38 +08:00
|
|
|
#include "common/printing.h"
|
|
|
|
|
#include "common/jsonconfig.h"
|
|
|
|
|
#include "common/stringUtils.h"
|
2023-06-01 11:37:52 +08:00
|
|
|
#include "detection/terminalshell/terminalshell.h"
|
|
|
|
|
#include "modules/shell/shell.h"
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
bool ffPrintShell(FFShellOptions* options) {
|
2024-01-12 20:42:33 +08:00
|
|
|
const FFShellResult* result = ffDetectShell();
|
2023-06-01 11:37:52 +08:00
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
if (result->processName.length == 0) {
|
2024-03-07 11:10:18 +08:00
|
|
|
ffPrintError(FF_SHELL_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Couldn't detect shell");
|
2025-08-18 13:38:30 +08:00
|
|
|
return false;
|
2023-06-01 11:37:52 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
if (options->moduleArgs.outputFormat.length == 0) {
|
2023-08-15 21:40:22 +08:00
|
|
|
ffPrintLogoAndKey(FF_SHELL_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
|
2024-01-12 20:42:33 +08:00
|
|
|
ffStrbufWriteTo(&result->prettyName, stdout);
|
2023-06-01 11:37:52 +08:00
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
if (result->version.length > 0) {
|
2023-06-01 11:37:52 +08:00
|
|
|
putchar(' ');
|
2024-01-12 20:42:33 +08:00
|
|
|
ffStrbufWriteTo(&result->version, stdout);
|
2023-06-01 11:37:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
putchar('\n');
|
2026-03-29 09:28:49 +08:00
|
|
|
} else {
|
2024-12-11 10:47:02 +08:00
|
|
|
FF_PRINT_FORMAT_CHECKED(FF_SHELL_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
|
2026-03-29 09:28:49 +08:00
|
|
|
FF_ARG(result->processName, "process-name"),
|
|
|
|
|
FF_ARG(result->exe, "exe"),
|
|
|
|
|
FF_ARG(result->exeName, "exe-name"),
|
|
|
|
|
FF_ARG(result->version, "version"),
|
|
|
|
|
FF_ARG(result->pid, "pid"),
|
|
|
|
|
FF_ARG(result->prettyName, "pretty-name"),
|
|
|
|
|
FF_ARG(result->exePath, "exe-path"),
|
|
|
|
|
FF_ARG(result->tty, "tty"),
|
|
|
|
|
}));
|
2023-06-01 11:37:52 +08:00
|
|
|
}
|
2025-08-18 13:38:30 +08:00
|
|
|
|
|
|
|
|
return true;
|
2023-06-01 11:37:52 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
void ffParseShellJsonObject(FFShellOptions* options, yyjson_val* module) {
|
2025-08-02 18:51:03 +08:00
|
|
|
yyjson_val *key, *val;
|
2023-08-17 16:09:41 +08:00
|
|
|
size_t idx, max;
|
2026-03-29 09:28:49 +08:00
|
|
|
yyjson_obj_foreach (module, idx, max, key, val) {
|
|
|
|
|
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
|
2023-08-17 16:09:41 +08:00
|
|
|
continue;
|
2026-03-29 09:28:49 +08:00
|
|
|
}
|
2023-06-01 11:37:52 +08:00
|
|
|
|
2025-08-02 18:51:03 +08:00
|
|
|
ffPrintError(FF_SHELL_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
|
2023-06-01 11:37:52 +08:00
|
|
|
}
|
|
|
|
|
}
|
2023-09-08 16:33:19 +08:00
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
void ffGenerateShellJsonConfig(FFShellOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
|
2025-08-04 16:46:28 +08:00
|
|
|
ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
|
2023-10-24 14:41:29 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-09 16:20:59 +08:00
|
|
|
bool ffGenerateShellJsonResult(FF_A_UNUSED FFShellOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
|
2024-01-12 20:42:33 +08:00
|
|
|
const FFShellResult* result = ffDetectShell();
|
2023-09-08 16:33:19 +08:00
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
if (result->processName.length == 0) {
|
2023-09-08 16:33:19 +08:00
|
|
|
yyjson_mut_obj_add_str(doc, module, "error", "Couldn't detect shell");
|
2025-08-18 13:38:30 +08:00
|
|
|
return false;
|
2023-09-08 16:33:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
|
2024-01-12 20:42:33 +08:00
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "exe", &result->exe);
|
|
|
|
|
yyjson_mut_obj_add_strcpy(doc, obj, "exeName", result->exeName);
|
2024-01-19 10:50:02 +08:00
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "exePath", &result->exePath);
|
2024-01-12 20:42:33 +08:00
|
|
|
yyjson_mut_obj_add_uint(doc, obj, "pid", result->pid);
|
|
|
|
|
yyjson_mut_obj_add_uint(doc, obj, "ppid", result->ppid);
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "processName", &result->processName);
|
2024-01-18 14:07:29 +08:00
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "prettyName", &result->prettyName);
|
2024-01-12 20:42:33 +08:00
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "version", &result->version);
|
2026-03-29 09:28:49 +08:00
|
|
|
if (result->tty >= 0) {
|
2024-01-19 15:42:48 +08:00
|
|
|
yyjson_mut_obj_add_int(doc, obj, "tty", result->tty);
|
2026-03-29 09:28:49 +08:00
|
|
|
} else {
|
2024-01-19 15:42:48 +08:00
|
|
|
yyjson_mut_obj_add_null(doc, obj, "tty");
|
2026-03-29 09:28:49 +08:00
|
|
|
}
|
2025-08-18 13:38:30 +08:00
|
|
|
|
|
|
|
|
return true;
|
2023-09-08 16:33:19 +08:00
|
|
|
}
|
2023-10-07 13:40:40 +08:00
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
void ffInitShellOptions(FFShellOptions* options) {
|
2025-08-04 14:25:14 +08:00
|
|
|
ffOptionInitModuleArg(&options->moduleArgs, "");
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
void ffDestroyShellOptions(FFShellOptions* options) {
|
2025-08-04 14:25:14 +08:00
|
|
|
ffOptionDestroyModuleArg(&options->moduleArgs);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-03 22:50:56 +08:00
|
|
|
FFModuleBaseInfo ffShellModuleInfo = {
|
2024-12-11 10:47:02 +08:00
|
|
|
.name = FF_SHELL_MODULE_NAME,
|
|
|
|
|
.description = "Print current shell name and version",
|
2025-08-03 18:39:41 +08:00
|
|
|
.initOptions = (void*) ffInitShellOptions,
|
|
|
|
|
.destroyOptions = (void*) ffDestroyShellOptions,
|
2024-12-11 10:47:02 +08:00
|
|
|
.parseJsonObject = (void*) ffParseShellJsonObject,
|
|
|
|
|
.printModule = (void*) ffPrintShell,
|
|
|
|
|
.generateJsonResult = (void*) ffGenerateShellJsonResult,
|
|
|
|
|
.generateJsonConfig = (void*) ffGenerateShellJsonConfig,
|
|
|
|
|
.formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
|
2026-03-30 10:27:23 +08:00
|
|
|
{ "Shell process name", "process-name" },
|
|
|
|
|
{ "The first argument of the command line when running the shell", "exe" },
|
|
|
|
|
{ "Shell base name of arg0", "exe-name" },
|
|
|
|
|
{ "Shell version", "version" },
|
|
|
|
|
{ "Shell pid", "pid" },
|
|
|
|
|
{ "Shell pretty name", "pretty-name" },
|
|
|
|
|
{ "Shell full exe path", "exe-path" },
|
|
|
|
|
{ "Shell tty used", "tty" },
|
2026-03-30 10:17:45 +08:00
|
|
|
}))
|
|
|
|
|
};
|