mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 20:11:17 +00:00
Escape arguments to platform linker/compiler. (#1358)
* Escape arguments to platform linker/compiler.
This commit is contained in:
committed by
GitHub
parent
2a69f93605
commit
85c682f7e6
@@ -320,37 +320,99 @@ void scratch_buffer_append_len(const char *string, size_t len)
|
||||
scratch_buffer.len += (uint32_t)len;
|
||||
}
|
||||
|
||||
char *scratch_buffer_get_quoted(const char *string)
|
||||
{
|
||||
scratch_buffer_clear();
|
||||
scratch_buffer_append_quoted(string);
|
||||
return scratch_buffer_to_string();
|
||||
}
|
||||
|
||||
void scratch_buffer_append_quoted(const char *string)
|
||||
#if PLATFORM_WINDOWS
|
||||
static bool contains_whitespace_or_quotes(const char *string)
|
||||
{
|
||||
char c;
|
||||
while ((c = string++[0]) != '\0')
|
||||
while ((c = *string++) != '\0')
|
||||
{
|
||||
switch (c)
|
||||
{
|
||||
case ' ':
|
||||
case '\t':
|
||||
case '\n':
|
||||
case '\r':
|
||||
case '"':
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
void scratch_buffer_append_argument(const char *string)
|
||||
{
|
||||
if (scratch_buffer.len != 0) scratch_buffer_append_char(' ');
|
||||
#if PLATFORM_WINDOWS
|
||||
if (contains_whitespace_or_quotes(string))
|
||||
{
|
||||
scratch_buffer_append_double_quoted(string);
|
||||
}
|
||||
else
|
||||
{
|
||||
scratch_buffer_append(string);
|
||||
}
|
||||
#else
|
||||
scratch_buffer_append_shell_escaped(string);
|
||||
#endif
|
||||
}
|
||||
|
||||
void scratch_buffer_append_double_quoted(const char *string)
|
||||
{
|
||||
scratch_buffer_append_char('"');
|
||||
size_t len = strlen(string);
|
||||
for (size_t i = 0; i < len; )
|
||||
{
|
||||
char c = string[i++];
|
||||
switch (c)
|
||||
{
|
||||
case '"':
|
||||
scratch_buffer_append("\\\"");
|
||||
continue;
|
||||
case '\\':
|
||||
scratch_buffer_append("\\\\");
|
||||
{
|
||||
int backslash_count = 1;
|
||||
for (; i < len && string[i] == '\\'; i++, backslash_count++) {}
|
||||
if (i == len || string[i] == '"')
|
||||
{
|
||||
scratch_buffer_append_char_repeat('\\', backslash_count * 2);
|
||||
}
|
||||
else
|
||||
{
|
||||
scratch_buffer_append_char_repeat('\\', backslash_count);
|
||||
}
|
||||
continue;
|
||||
case '\n':
|
||||
scratch_buffer_append("\\n");
|
||||
continue;
|
||||
case '\'':
|
||||
scratch_buffer_append("\\'");
|
||||
continue;
|
||||
default:
|
||||
scratch_buffer_append_char(c);
|
||||
continue;
|
||||
|
||||
}
|
||||
}
|
||||
scratch_buffer_append_char(c);
|
||||
}
|
||||
scratch_buffer_append_char('"');
|
||||
}
|
||||
|
||||
void scratch_buffer_append_shell_escaped(const char *string)
|
||||
{
|
||||
char c;
|
||||
while ((c = string++[0]) != '\0')
|
||||
{
|
||||
if ((unsigned)c < 0x80)
|
||||
{
|
||||
switch (c)
|
||||
{
|
||||
case LOWER_CHAR_CASE:
|
||||
case UPPER_CHAR_CASE:
|
||||
case NUMBER_CHAR_CASE:
|
||||
case '_':
|
||||
case '/':
|
||||
case '.':
|
||||
case ',':
|
||||
case '-':
|
||||
break;
|
||||
default:
|
||||
scratch_buffer_append_char('\\');
|
||||
break;
|
||||
}
|
||||
}
|
||||
scratch_buffer_append_char(c);
|
||||
}
|
||||
}
|
||||
void scratch_buffer_append(const char *string)
|
||||
@@ -407,6 +469,14 @@ void scratch_buffer_append_char(char c)
|
||||
scratch_buffer.str[scratch_buffer.len++] = c;
|
||||
}
|
||||
|
||||
void scratch_buffer_append_char_repeat(char c, size_t count)
|
||||
{
|
||||
for (size_t i = 0; i < count; i++)
|
||||
{
|
||||
scratch_buffer_append_char(c);
|
||||
}
|
||||
}
|
||||
|
||||
char *scratch_buffer_to_string(void)
|
||||
{
|
||||
scratch_buffer.str[scratch_buffer.len] = '\0';
|
||||
|
||||
Reference in New Issue
Block a user