From d9125d76cb917397212f106f9b8eb8632f6ab71a Mon Sep 17 00:00:00 2001 From: Greg Ercolano Date: Thu, 11 Jan 2024 07:50:09 -0800 Subject: Solves issue #884: single quote zenity/kdialog --- src/Fl_Native_File_Chooser_Kdialog.H | 4 +- src/Fl_Native_File_Chooser_Kdialog.cxx | 55 ++++++++++------ src/Fl_Native_File_Chooser_Zenity.H | 3 +- src/Fl_Native_File_Chooser_Zenity.cxx | 117 ++++++++++++++++++--------------- 4 files changed, 105 insertions(+), 74 deletions(-) (limited to 'src') diff --git a/src/Fl_Native_File_Chooser_Kdialog.H b/src/Fl_Native_File_Chooser_Kdialog.H index f65445fb3..8b50b36fa 100644 --- a/src/Fl_Native_File_Chooser_Kdialog.H +++ b/src/Fl_Native_File_Chooser_Kdialog.H @@ -24,6 +24,7 @@ */ #include +#include "Fl_String.H" class Fl_Kdialog_Native_File_Chooser_Driver : public Fl_Native_File_Chooser_FLTK_Driver { friend class Fl_Native_File_Chooser; @@ -45,7 +46,7 @@ class Fl_Kdialog_Native_File_Chooser_Driver : public Fl_Native_File_Chooser_FLTK int count() const FL_OVERRIDE; const char *filename() const FL_OVERRIDE; const char *filename(int i) const FL_OVERRIDE; - virtual char *build_command(); + virtual void build_command(Fl_String& command); int show() FL_OVERRIDE; char *parse_filter(const char *f); const char *filter() const FL_OVERRIDE; @@ -57,6 +58,7 @@ class Fl_Kdialog_Native_File_Chooser_Driver : public Fl_Native_File_Chooser_FLTK const char *directory() const FL_OVERRIDE; void title(const char *val) FL_OVERRIDE; const char *title() const FL_OVERRIDE; + void shell_quote(Fl_String& s); }; /** 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 #include - /* 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 /dev/null"); // get rid of stderr output -//puts(command); - return command; + if (_parsedfilt) append_filter(command); + command += " 2> /dev/null"; // get rid of stderr + //printf("command = %s\n", command.c_str()); } /** -- cgit v1.2.3