cinera.c: Gracefully handle unset env variables

This commit is contained in:
Matt Mascarenhas 2020-06-24 16:22:50 +01:00
parent 2748687839
commit b3470e0f48
2 changed files with 170 additions and 114 deletions

View File

@ -23,7 +23,7 @@ typedef struct
version CINERA_APP_VERSION = {
.Major = 0,
.Minor = 7,
.Patch = 16
.Patch = 17
};
#include <stdarg.h> // NOTE(matt): varargs
@ -1825,12 +1825,14 @@ char *ErrorDomainStrings[] =
{
"Config",
"Indexing",
"System",
};
typedef enum
{
ED_CONFIG,
ED_INDEXING,
ED_SYSTEM,
} error_domain;
void
@ -2014,6 +2016,20 @@ IndexingErrorCustomSizing(string *Filename, uint64_t LineNumber, int CustomIndex
}
}
void
SystemError(string *Filename, uint64_t LineNumber, severity Severity, char *Message, string *Received)
{
ErrorFilenameAndLineNumber(Filename, LineNumber, Severity, ED_SYSTEM);
// TODO(matt): Typeset the Message?
fprintf(stderr,
"%s", Message);
if(Received)
{
PrintStringC(CS_MAGENTA_BOLD, *Received);
}
fprintf(stderr, "\n");
}
typedef enum
{
// NOTE(matt): https://tools.ietf.org/html/rfc5424#section-6.2.1
@ -3877,7 +3893,35 @@ ExpandPath(string Path, string *RelativeToFile)
wordexp_t Expansions = {};
char *WorkingPath = MakeString0("l", &Path);
wordexp(WorkingPath, &Expansions, Flags);
int wordexpResult = wordexp(WorkingPath, &Expansions, Flags);
if(wordexpResult)
{
switch(wordexpResult)
{
case WRDE_BADCHAR:
{
SystemError(0, 0, S_ERROR, "wordexp: Illegal occurrence of newline or one of |, &, ;, <, >, (, ), {, } in: ", &Path);
} break;
case WRDE_BADVAL:
{
SystemError(0, 0, S_ERROR, "wordexp: An undefined shell variable was referenced, and the WRDE_UNDEF flag told us to consider this an error, in: ", &Path);
} break;
case WRDE_CMDSUB:
{
SystemError(0, 0, S_ERROR, "wordexp: Command substitution requested, but the WRDE_NOCMD flag told us to consider this an error, in: ", &Path);
} break;
case WRDE_NOSPACE:
{
SystemError(0, 0, S_ERROR, "wordexp: Out of memory", 0);
} break;
case WRDE_SYNTAX:
{
SystemError(0, 0, S_ERROR, "wordexp: Shell syntax error, such as unbalanced parentheses or unmatched quotes, in: ", &Path);
} break;
}
}
Free(WorkingPath);
char *Result = 0;
@ -3903,7 +3947,10 @@ ExpandPath(string Path, string *RelativeToFile)
wordfree(&Expansions);
if(Result)
{
ResolvePath(&Result);
}
return Result;
}
@ -15719,7 +15766,8 @@ main(int ArgC, char **Args)
MEM_TEST_MID("main()");
ConfigPath = ExpandPath(Wrap0(ConfigPath), 0);
if(ConfigPath)
{
PrintVersions();
#if DEBUG_MEM
@ -15826,5 +15874,6 @@ main(int ArgC, char **Args)
DiscardAllAndFreeConfig();
RemoveAndFreeWatchHandles(&WatchHandles);
MEM_TEST_END("main()");
}
Exit();
}

View File

@ -2280,6 +2280,8 @@ ScopeTokens(scope_tree *Tree, memory_book *TokensList, tokens *T, memory_book *T
{
string IncluderFilePath = Wrap0(T->File.Path);
char *IncludePath = ExpandPath(Pair.Value, &IncluderFilePath);
if(IncludePath)
{
string IncludePathL = Wrap0(IncludePath);
tokens *I = 0;
@ -2312,6 +2314,11 @@ ScopeTokens(scope_tree *Tree, memory_book *TokensList, tokens *T, memory_book *T
ConfigFileIncludeError(&Filepath, This->LineNumber, Wrap0(IncludePath));
}
Free(IncludePath);
}
else
{
ConfigError(&IncluderFilePath, Pair.Position.LineNumber, S_WARNING, "Unable to include file: ", &Pair.Value);
}
} break;
case RC_SCHEME_MIXTURE:
{