Skip to content

Commit 3e7630e

Browse files
committed
Fastfetch: enforce .jsonc extension when loading config file
Examples: 1. `-c filename`: load `filename.jsonc` 2. `-c filename.jsonc`: load `filename.jsonc` 3. `-c filename.json`: load `filename.json` and enforce strict JSON syntax (no comment and trailing comma support) 4. `-c filename.ext`: load `filename.ext.jsonc` (`jsonc` extension is enforced)
1 parent 744455d commit 3e7630e

File tree

1 file changed

+35
-40
lines changed

1 file changed

+35
-40
lines changed

Diff for: src/fastfetch.c

+35-40
Original file line numberDiff line numberDiff line change
@@ -374,13 +374,13 @@ static void listModules(bool pretty)
374374
}
375375
}
376376

377-
static bool parseJsoncFile(const char* path)
377+
static bool parseJsoncFile(const char* path, bool strictJson)
378378
{
379379
assert(!instance.state.configDoc);
380380

381381
{
382382
yyjson_read_err error;
383-
instance.state.configDoc = yyjson_read_file(path, YYJSON_READ_ALLOW_COMMENTS | YYJSON_READ_ALLOW_TRAILING_COMMAS, NULL, &error);
383+
instance.state.configDoc = yyjson_read_file(path, strictJson ? 0 : YYJSON_READ_ALLOW_COMMENTS | YYJSON_READ_ALLOW_TRAILING_COMMAS, NULL, &error);
384384
if (!instance.state.configDoc)
385385
{
386386
if (error.code != YYJSON_READ_ERROR_FILE_OPEN)
@@ -452,57 +452,52 @@ static void optionParseConfigFile(FFdata* data, const char* key, const char* val
452452
fprintf(stderr, "Error: usage: %s <config>\n", key);
453453
exit(413);
454454
}
455-
uint32_t fileNameLen = (uint32_t) strlen(value);
456-
if(fileNameLen == 0)
457-
{
458-
fprintf(stderr, "Error: usage: %s <config>\n", key);
459-
exit(413);
460-
}
461455

462-
if (ffStrEqualsIgnCase(value, "none"))
456+
if (value[0] == '\0' || ffStrEqualsIgnCase(value, "none"))
463457
return;
464458

465-
if (ffStrEndsWithIgnCase(value, ".conf"))
466-
{
467-
fprintf(stderr, "Error: flag based config files are no longer not supported: %s\n", value);
468-
exit(414);
469-
}
470-
471459
//Try to load as an absolute path
472460

473-
if (parseJsoncFile(value)) return;
461+
FF_STRBUF_AUTO_DESTROY absolutePath = ffStrbufCreateS(value);
462+
bool strictJson = ffStrbufEndsWithIgnCaseS(&absolutePath, ".json");
463+
bool needExtension = !strictJson && !ffStrbufEndsWithIgnCaseS(&absolutePath, ".jsonc");
464+
if (needExtension)
465+
ffStrbufAppendS(&absolutePath, ".jsonc");
474466

475-
FF_STRBUF_AUTO_DESTROY absolutePath = ffStrbufCreateA(128);
467+
if (parseJsoncFile(absolutePath.chars, strictJson)) return;
476468

477-
//Try to load as a relative path with the directory of fastfetch binary
478-
if (instance.state.platform.exePath.length)
469+
if (!ffStrbufContainC(&absolutePath, '/')
470+
#ifdef _WIN32
471+
&& !ffStrbufContainC(&absolutePath, '\\')
472+
#endif
473+
)
479474
{
480-
ffStrbufSet(&absolutePath, &instance.state.platform.exePath);
481-
ffStrbufSubstrBeforeLastC(&absolutePath, '/');
482-
ffStrbufAppendS(&absolutePath, "/");
483-
ffStrbufAppendS(&absolutePath, value);
475+
//Try to load as a relative path
484476

485-
if (parseJsoncFile(absolutePath.chars)) return;
486-
ffStrbufClear(&absolutePath);
487-
}
477+
FF_LIST_FOR_EACH(FFstrbuf, path, instance.state.platform.dataDirs)
478+
{
479+
ffStrbufSet(&absolutePath, path);
480+
ffStrbufAppendS(&absolutePath, "fastfetch/presets/");
481+
ffStrbufAppendS(&absolutePath, value);
482+
if (needExtension)
483+
ffStrbufAppendS(&absolutePath, ".jsonc");
488484

489-
//Try to load as a relative path
485+
if (parseJsoncFile(absolutePath.chars, strictJson)) return;
486+
}
490487

491-
FF_LIST_FOR_EACH(FFstrbuf, path, instance.state.platform.dataDirs)
492-
{
493-
//We need to copy it, because if a config file loads a config file, the value of path must be unchanged
494-
ffStrbufSet(&absolutePath, path);
495-
ffStrbufAppendS(&absolutePath, "fastfetch/presets/");
496-
ffStrbufAppendS(&absolutePath, value);
488+
//Try to load as a relative path with the directory of fastfetch binary
497489

498-
bool success = parseJsoncFile(absolutePath.chars);
499-
if (!success)
490+
if (instance.state.platform.exePath.length)
500491
{
501-
ffStrbufAppendS(&absolutePath, ".jsonc");
502-
success = parseJsoncFile(absolutePath.chars);
492+
ffStrbufSet(&absolutePath, &instance.state.platform.exePath);
493+
ffStrbufSubstrBeforeLastC(&absolutePath, '/');
494+
ffStrbufAppendS(&absolutePath, "/");
495+
ffStrbufAppendS(&absolutePath, value);
496+
if (needExtension)
497+
ffStrbufAppendS(&absolutePath, ".jsonc");
498+
499+
if (parseJsoncFile(absolutePath.chars, strictJson)) return;
503500
}
504-
505-
if (success) return;
506501
}
507502

508503
//File not found
@@ -665,7 +660,7 @@ static void parseConfigFiles(void)
665660
uint32_t dirLength = dir->length;
666661

667662
ffStrbufAppendS(dir, "fastfetch/config.jsonc");
668-
bool success = parseJsoncFile(dir->chars);
663+
bool success = parseJsoncFile(dir->chars, false);
669664
ffStrbufSubstrBefore(dir, dirLength);
670665
if (success) return;
671666
}

0 commit comments

Comments
 (0)