summaryrefslogtreecommitdiff
path: root/src/Fl_Native_File_Chooser_Kdialog.cxx
diff options
context:
space:
mode:
authorGreg Ercolano <erco@seriss.com>2024-01-11 07:50:09 -0800
committerGreg Ercolano <erco@seriss.com>2024-01-11 07:50:09 -0800
commitd9125d76cb917397212f106f9b8eb8632f6ab71a (patch)
treef33ba86c0e48dabb07dc03c3e2dcd5599911d62b /src/Fl_Native_File_Chooser_Kdialog.cxx
parenta237743c8babfe5a24acd9b3ab7aa79dcf0159ad (diff)
Solves issue #884: single quote zenity/kdialog
Diffstat (limited to 'src/Fl_Native_File_Chooser_Kdialog.cxx')
-rw-r--r--src/Fl_Native_File_Chooser_Kdialog.cxx55
1 files changed, 36 insertions, 19 deletions
diff --git a/src/Fl_Native_File_Chooser_Kdialog.cxx b/src/Fl_Native_File_Chooser_Kdialog.cxx
index 72bb6ce18..1fa322c98 100644
--- a/src/Fl_Native_File_Chooser_Kdialog.cxx
+++ b/src/Fl_Native_File_Chooser_Kdialog.cxx
@@ -30,7 +30,6 @@
#include <string.h>
#include <unistd.h>
-
/* Fl_Kdialog_Native_File_Chooser_Driver : file chooser based on the "kdialog" command */
bool Fl_Kdialog_Native_File_Chooser_Driver::did_find_kdialog = false;
@@ -73,7 +72,7 @@ static int fnfc_dispatch(int /*event*/, Fl_Window* /*win*/) {
}
-char *Fl_Kdialog_Native_File_Chooser_Driver::build_command() {
+void Fl_Kdialog_Native_File_Chooser_Driver::build_command(Fl_String& command) {
const char *option;
switch (_btype) {
case Fl_Native_File_Chooser::BROWSE_DIRECTORY:
@@ -92,25 +91,30 @@ char *Fl_Kdialog_Native_File_Chooser_Driver::build_command() {
default:
option = "--getopenfilename";
}
+
+ // Build preset
const char *preset = ".";
- if (_preset_file) preset = _preset_file;
+ if (_preset_file) preset = _preset_file;
else if (_directory) preset = _directory;
- const int com_size = strlen(option) + strlen(preset) +
- (_title?strlen(_title)+11:0) + (_parsedfilt?strlen(_parsedfilt):0) + 50;
- char *command = new char[com_size];
- strcpy(command, "kdialog ");
+
+ // Build command
+ command = "kdialog";
if (_title) {
- snprintf(command+strlen(command), com_size - strlen(command),
- " --title '%s'", _title);
+ Fl_String quoted_title = _title; shell_quote(quoted_title);
+ command += " --title ";
+ command += quoted_title;
}
- snprintf(command+strlen(command), com_size - strlen(command),
- " %s %s ", option, preset);
+ command += " ";
+ command += option;
+ command += " ";
+ command += preset;
if (_parsedfilt) {
- snprintf(command+strlen(command), com_size - strlen(command),
- " \"%s\" ", _parsedfilt);
+ Fl_String quoted_filt = _parsedfilt; shell_quote(quoted_filt); // NOTE: orig code used double quoting -erco 1/10/24
+ command += " ";
+ command += quoted_filt;
}
- strcat(command, "2> /dev/null"); // get rid of stderr output
- return command;
+ command += " 2> /dev/null"; // get rid of stderr
+ //printf("command = %s\n", command.c_str());
}
@@ -137,9 +141,10 @@ int Fl_Kdialog_Native_File_Chooser_Driver::show() {
return retval;
}
- char *command = build_command();
-//puts(command);
- FILE *pipe = popen(command, "r");
+ Fl_String command;
+ build_command(command);
+ fprintf(stderr, "DEBUG: POPEN: %s\n", command.c_str());
+ FILE *pipe = popen(command.c_str(), "r");
fnfc_pipe_struct data;
data.all_files = NULL;
if (pipe) {
@@ -175,7 +180,6 @@ int Fl_Kdialog_Native_File_Chooser_Driver::show() {
}
}
}
- delete[] command;
if (!pipe) return -1;
return (data.all_files == NULL ? 1 : 0);
}
@@ -298,6 +302,19 @@ const char *Fl_Kdialog_Native_File_Chooser_Driver::title() const {
return _title;
}
+// Add shell quotes around string 's'.
+// Handles quoting embedded quotes.
+//
+void Fl_Kdialog_Native_File_Chooser_Driver::shell_quote(Fl_String& s) {
+ Fl_String out = "'"; // leading quote
+ for (int t=0; t<s.size(); t++) {
+ if (s[t] == '\'') out += "'\"'\"'"; // quote any quotes
+ else out += s[t];
+ }
+ out += "'"; // trailing quote
+ s = out;
+}
+
/**
\}
\endcond