mirror of
https://github.com/CalcProgrammer1/OpenRGB.git
synced 2026-09-13 05:39:07 -04:00
Update MacOS and Linux AutoStart implementations to properly escape special characters in paths
This commit is contained in:
1 parent
41b7087e20
commit
ecf0bfc847
2 files changed
+107
-4
No files matched your search
@@ -149,6 +149,64 @@ std::string AutoStart::GetExePath()
|
||||
| Private Methods |
|
||||
\*---------------------------------------------------------*/
|
||||
|
||||
/*---------------------------------------------------------*\
|
||||
| Escape a single argument for use in a .desktop Exec= |
|
||||
| field, per the Desktop Entry Specification. |
|
||||
| |
|
||||
| Reserved characters must be escaped by wrapping the whole |
|
||||
| argument in double quotes and backslash-escaping the |
|
||||
| quote/backquote/backslash/dollar characters, and any |
|
||||
| literal '%' must be doubled to '%%'. |
|
||||
\*---------------------------------------------------------*/
|
||||
static std::string DesktopExecEscape(const std::string& arg)
|
||||
{
|
||||
static const std::string reserved = " \t\n\"'`\\><~|&;$*?#()%";
|
||||
|
||||
bool needs_quotes = false;
|
||||
for(char c : arg)
|
||||
{
|
||||
if(reserved.find(c) != std::string::npos)
|
||||
{
|
||||
needs_quotes = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
std::string escaped;
|
||||
|
||||
if(needs_quotes)
|
||||
{
|
||||
escaped += '"';
|
||||
}
|
||||
|
||||
for(char c : arg)
|
||||
{
|
||||
switch(c)
|
||||
{
|
||||
case '"':
|
||||
case '`':
|
||||
case '\\':
|
||||
case '$':
|
||||
escaped += '\\';
|
||||
escaped += c;
|
||||
break;
|
||||
case '%':
|
||||
escaped += "%%";
|
||||
break;
|
||||
default:
|
||||
escaped += c;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(needs_quotes)
|
||||
{
|
||||
escaped += '"';
|
||||
}
|
||||
|
||||
return(escaped);
|
||||
}
|
||||
|
||||
std::string AutoStart::GenerateDesktopFile(AutoStartInfo autostart_info)
|
||||
{
|
||||
/*-----------------------------------------------------*\
|
||||
@@ -169,11 +227,17 @@ std::string AutoStart::GenerateDesktopFile(AutoStartInfo autostart_info)
|
||||
/*-----------------------------------------------------*\
|
||||
| Add the executable path and arguments |
|
||||
\*-----------------------------------------------------*/
|
||||
fileContents << "Exec=" << autostart_info.path;
|
||||
fileContents << "Exec=" << DesktopExecEscape(autostart_info.path);
|
||||
|
||||
if (autostart_info.args != "")
|
||||
{
|
||||
fileContents << " " << autostart_info.args;
|
||||
std::istringstream arg_parser(autostart_info.args);
|
||||
std::string arg;
|
||||
|
||||
while(arg_parser >> arg)
|
||||
{
|
||||
fileContents << " " << DesktopExecEscape(arg);
|
||||
}
|
||||
}
|
||||
|
||||
fileContents << std::endl;
|
||||
|
||||
Reference in new issue
Block a user