|
| 1 | +#include "common/printing.h" |
| 2 | +#include "common/jsonconfig.h" |
| 3 | +#include "detection/libc/libc.h" |
| 4 | +#include "detection/dotfilemanager/dotfilemanager.h" |
| 5 | +#include "modules/dotfilemanager/dotfilemanager.h" |
| 6 | +#include "util/stringUtils.h" |
| 7 | + |
| 8 | +void ffPrintDotfileManager(FFDotfileManagerOptions* options) |
| 9 | +{ |
| 10 | + FFDotfileManagerResult result = { |
| 11 | + .name = ffStrbufCreate(), |
| 12 | + }; |
| 13 | + const char* error = ffDetectDotfileManager(&result); |
| 14 | + |
| 15 | + if (error) |
| 16 | + { |
| 17 | + ffPrintError(FF_DOTFILEMANAGER_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); |
| 18 | + return; |
| 19 | + } |
| 20 | + |
| 21 | + if (options->moduleArgs.outputFormat.length == 0) |
| 22 | + { |
| 23 | + ffPrintLogoAndKey(FF_DOTFILEMANAGER_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT); |
| 24 | + ffStrbufWriteTo(&result.name, stdout); |
| 25 | + putchar('\n'); |
| 26 | + } |
| 27 | + else |
| 28 | + { |
| 29 | + FF_PRINT_FORMAT_CHECKED(FF_DOTFILEMANAGER_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]){ |
| 30 | + FF_FORMAT_ARG(result.name, "name"), |
| 31 | + })); |
| 32 | + } |
| 33 | + |
| 34 | + ffStrbufDestroy(&result.name); |
| 35 | +} |
| 36 | + |
| 37 | +bool ffParseDotfileManagerCommandOptions(FFDotfileManagerOptions* options, const char* key, const char* value) |
| 38 | +{ |
| 39 | + const char* subKey = ffOptionTestPrefix(key, FF_DOTFILEMANAGER_MODULE_NAME); |
| 40 | + if (!subKey) return false; |
| 41 | + if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs)) |
| 42 | + return true; |
| 43 | + |
| 44 | + return false; |
| 45 | +} |
| 46 | + |
| 47 | +void ffParseDotfileManagerJsonObject(FFDotfileManagerOptions* options, yyjson_val* module) |
| 48 | +{ |
| 49 | + yyjson_val *key_, *val; |
| 50 | + size_t idx, max; |
| 51 | + yyjson_obj_foreach(module, idx, max, key_, val) |
| 52 | + { |
| 53 | + const char* key = yyjson_get_str(key_); |
| 54 | + if(ffStrEqualsIgnCase(key, "type")) |
| 55 | + continue; |
| 56 | + |
| 57 | + if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) |
| 58 | + continue; |
| 59 | + |
| 60 | + ffPrintError(FF_DOTFILEMANAGER_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", key); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +void ffGenerateDotfileManagerJsonConfig(FFDotfileManagerOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) |
| 65 | +{ |
| 66 | + __attribute__((__cleanup__(ffDestroyDotfileManagerOptions))) FFDotfileManagerOptions defaultOptions; |
| 67 | + ffInitDotfileManagerOptions(&defaultOptions); |
| 68 | + |
| 69 | + ffJsonConfigGenerateModuleArgsConfig(doc, module, &defaultOptions.moduleArgs, &options->moduleArgs); |
| 70 | +} |
| 71 | + |
| 72 | +void ffGenerateDotfileManagerJsonResult(FF_MAYBE_UNUSED FFDotfileManagerOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) |
| 73 | +{ |
| 74 | + FFDotfileManagerResult result = { |
| 75 | + .name = ffStrbufCreate(), |
| 76 | + }; |
| 77 | + |
| 78 | + const char* error = ffDetectDotfileManager(&result); |
| 79 | + |
| 80 | + if (error) |
| 81 | + { |
| 82 | + yyjson_mut_obj_add_str(doc, module, "error", error); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result"); |
| 87 | + yyjson_mut_obj_add_strbuf(doc, obj, "name", &result.name); |
| 88 | + |
| 89 | + ffStrbufDestroy(&result.name); |
| 90 | +} |
| 91 | + |
| 92 | +static FFModuleBaseInfo ffModuleInfo = { |
| 93 | + .name = FF_DOTFILEMANAGER_MODULE_NAME, |
| 94 | + .description = "Print information about the dotfile manager", |
| 95 | + .parseCommandOptions = (void*) ffParseDotfileManagerCommandOptions, |
| 96 | + .parseJsonObject = (void*) ffParseDotfileManagerJsonObject, |
| 97 | + .printModule = (void*) ffPrintDotfileManager, |
| 98 | + .generateJsonResult = (void*) ffGenerateDotfileManagerJsonResult, |
| 99 | + .generateJsonConfig = (void*) ffGenerateDotfileManagerJsonConfig, |
| 100 | + .formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) { |
| 101 | + {"Name", "name"}, |
| 102 | + })) |
| 103 | +}; |
| 104 | + |
| 105 | +void ffInitDotfileManagerOptions(FFDotfileManagerOptions* options) |
| 106 | +{ |
| 107 | + options->moduleInfo = ffModuleInfo; |
| 108 | + ffOptionInitModuleArg(&options->moduleArgs, ""); |
| 109 | +} |
| 110 | + |
| 111 | +void ffDestroyDotfileManagerOptions(FFDotfileManagerOptions* options) |
| 112 | +{ |
| 113 | + ffOptionDestroyModuleArg(&options->moduleArgs); |
| 114 | +} |
0 commit comments