summaryrefslogtreecommitdiff
path: root/src/Fl_Native_File_Chooser_Zenity.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_Zenity.cxx
parenta237743c8babfe5a24acd9b3ab7aa79dcf0159ad (diff)
Solves issue #884: single quote zenity/kdialog
Diffstat (limited to 'src/Fl_Native_File_Chooser_Zenity.cxx')
-rw-r--r--src/Fl_Native_File_Chooser_Zenity.cxx117
1 files changed, 64 insertions, 53 deletions
diff --git a/src/Fl_Native_File_Chooser_Zenity.cxx b/src/Fl_Native_File_Chooser_Zenity.cxx
index aa967bfb0..908e68cda 100644
--- a/src/Fl_Native_File_Chooser_Zenity.cxx
+++ b/src/Fl_Native_File_Chooser_Zenity.cxx
@@ -36,10 +36,48 @@ bool Fl_Zenity_Native_File_Chooser_Driver::have_looked_for_zenity = false;
Fl_Zenity_Native_File_Chooser_Driver::Fl_Zenity_Native_File_Chooser_Driver(int val) : Fl_Kdialog_Native_File_Chooser_Driver(val) {
}
+void Fl_Zenity_Native_File_Chooser_Driver::append_filter(Fl_String& ret_command) {
+ // TODO: This could probably be simplified + toughened with Fl_String -erco 1/10/24
+ int l;
+ int lcommand = 10000;
+ char *command = new char[lcommand];
+ command[0] = 0;
+ char *parsed_filter_copy = strdup(_parsedfilt); // keep _parsedfilt unchanged for re-use
+ char *p = strtok(parsed_filter_copy, "\n");
+ while (p) {
+ char *op = strchr(p, '(');
+ l = strlen(command);
+ snprintf(command+l, lcommand-l, " --file-filter='%s|", p);
+ char *cp = strchr(p, ')');
+ *cp = 0;
+ char *ob = strchr(op+1, '[');
+ if (ob) { // process [xyz] patterns
+ *ob = 0;
+ char *cb = strchr(ob+1, ']');
+ char aux[100];
+ for (char *q = ob+1; q < cb; q++) {
+ strcpy(aux, op+1);
+ int la = strlen(aux);
+ aux[la++] = *q;
+ if (cb < cp-1) { strcpy(aux+la, cb+1); la += strlen(cb+1); }
+ aux[la] = 0;
+ l = strlen(command);
+ snprintf(command+l, lcommand-l, " %s", aux);
+ }
+ strcat(command, "'");
+ } else {
+ l = strlen(command);
+ snprintf(command+l, lcommand-l, "%s'", op+1);
+ }
+ p = strtok(NULL, "\n");
+ }
+ free(parsed_filter_copy);
+ ret_command += command; // append to parent's Fl_String
+ delete [] command;
+}
-char *Fl_Zenity_Native_File_Chooser_Driver::build_command() {
+void Fl_Zenity_Native_File_Chooser_Driver::build_command(Fl_String& command) {
const char *option;
- int l;
switch (_btype) {
case Fl_Native_File_Chooser::BROWSE_DIRECTORY:
case Fl_Native_File_Chooser::BROWSE_SAVE_DIRECTORY:
@@ -60,63 +98,36 @@ char *Fl_Zenity_Native_File_Chooser_Driver::build_command() {
default:
option = "--file-selection";
}
- char *preset = NULL;
+
+ // Build preset
+ Fl_String preset;
if (_preset_file) {
- l = strlen(_preset_file) + 15;
- preset = new char[l];
- snprintf(preset, l, "--filename='%s'", _preset_file);
- }
- else if (_directory) {
+ Fl_String quoted_filename = _preset_file; shell_quote(quoted_filename);
+ preset = "--filename=";
+ preset += quoted_filename;
+ } else if (_directory) {
// This doesn't actually seem to do anything, but supply it anyway.
- l = strlen(_directory) + 15;
- preset = new char[l];
- snprintf(preset, l, "--filename '%s'", _directory);
+ Fl_String quoted_dir = _directory; shell_quote(quoted_dir);
+ preset = "--filename=";
+ preset += quoted_dir;
}
- int lcommand = 10000;
- char *command = new char[lcommand];
- strcpy(command, "zenity ");
+
+ // Build command
+ command = "zenity";
if (_title) {
- l = strlen(command);
- snprintf(command+l, lcommand-l, " --title '%s'", _title);
+ Fl_String quoted_title = _title; shell_quote(quoted_title);
+ command += " --title ";
+ command += quoted_title;
}
- l = strlen(command);
- snprintf(command+l, lcommand-l, " %s %s ", option, preset ? preset : "");
- delete[] preset;
- if (_parsedfilt) {
- char *parsed_filter_copy = strdup(_parsedfilt); // keep _parsedfilt unchanged for re-use
- char *p = strtok(parsed_filter_copy, "\n");
- while (p) {
- char *op = strchr(p, '(');
- l = strlen(command);
- snprintf(command+l, lcommand-l, " --file-filter='%s|", p);
- char *cp = strchr(p, ')');
- *cp = 0;
- char *ob = strchr(op+1, '[');
- if (ob) { // process [xyz] patterns
- *ob = 0;
- char *cb = strchr(ob+1, ']');
- char aux[100];
- for (char *q = ob+1; q < cb; q++) {
- strcpy(aux, op+1);
- int la = strlen(aux);
- aux[la++] = *q;
- if (cb < cp-1) { strcpy(aux+la, cb+1); la += strlen(cb+1); }
- aux[la] = 0;
- l = strlen(command);
- snprintf(command+l, lcommand-l, " %s", aux);
- }
- strcat(command, "'");
- } else {
- l = strlen(command);
- snprintf(command+l, lcommand-l, "%s'", op+1);
- }
- p = strtok(NULL, "\n");
- }
- free(parsed_filter_copy);
+ command += " ";
+ command += option;
+ if (preset != "") {
+ command += " ";
+ command += preset;
}
- strcat(command, " 2> /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());
}
/**