diff options
| author | maxim nikonov <maxim.nikonov@hqo.co> | 2026-02-06 02:33:41 +0500 |
|---|---|---|
| committer | maxim nikonov <maxim.nikonov@hqo.co> | 2026-02-06 02:33:41 +0500 |
| commit | 43e0a37906afabb0b3b091b8d3eac9a910cae50c (patch) | |
| tree | d2a037c2bf0dc395fddb08e32ebfcf2795503b7c /fluid/app/shell_command.cxx | |
| parent | 4ce4967c33d56e4b56d85d11fe0e0be91e159f5d (diff) | |
wip
Diffstat (limited to 'fluid/app/shell_command.cxx')
| -rw-r--r-- | fluid/app/shell_command.cxx | 537 |
1 files changed, 277 insertions, 260 deletions
diff --git a/fluid/app/shell_command.cxx b/fluid/app/shell_command.cxx index 05bc28ec0..0a9a04121 100644 --- a/fluid/app/shell_command.cxx +++ b/fluid/app/shell_command.cxx @@ -1,5 +1,5 @@ // -// Shell Command database coe for the Fast Light Tool Kit (FLTK). +// Shell Command database code for the Fast Light Tool Kit (FLTK). // // Copyright 1998-2025 by Bill Spitzak and others. // @@ -14,87 +14,6 @@ // https://www.fltk.org/bugs.php // -// in progress: -// FLUID comes with example shell commands to build the current project file -// and run the project. This is accomplished by calling `fltk-config` on the -// files generated by FLUID, and by calling the executable directly. -// -// If the user wants more complex commands, he can add or modify them in the -// "Shell" settings panel. Modified shell commands are saved with the .fl -// file. - -// The Shell panel has a list of shell commands in the upper half. Under the -// list are buttons to add, duplicate, and delete shell commands. A popup -// menu offers import and export functionality and a list of sample scripts. -// We may want to add up and down buttons, so the user can change the -// order of commands. - -// Selecting any shell command in the list fills in and activates a list of -// options in the lower half of the panel. Those settings are: -// - Name: the name of the shell command in the list -// - Label: the label in the pulldown menu (could be the same as name?) -// - Shortcut: shortcut key to launch the command -// - Storage: where to store this shell command -// - Condition: pulldown menu to make the entry conditional for various -// target platforms, for example, a "Windows only" entry would only be added -// to the Shell menu on a Windows machine. Other options could be: -// - Linux only, macOS only, never (to make a list header!?), inactive? -// - Command: a multiline input for the actual shell command -// - Variables: a pulldown menu that insert variable names like $<sourcefile> -// - options to save project, code, and strings before running -// - test-run button - -// TODO: add @APPDIR@? -// TODO: get a macro to find `fltk-config` @FLTK_CONFIG@ -// TODO: add an input field so the user can insert their preferred file and path for fltk-config (user setting) -// `fltk-config` is actually tricky to find -// for live builds, we could check the program launch directory -// if we know where build/Xcode/bin/Debug/fluid is, we -// may or may not find ./build/Xcode/fltk-config -// on macOS with homebrew, we find /opt/homebrew/bin/fltk-config but the user -// can set their own install path. -// We can query the shell path, but that requires knowing the users shell (echo $SHELL). -// We can run the shell as a login shell with `-l`, so the user $PTH is set: /bin/bash -l -c 'fltk-config' -// The shell should output the path of the fltk-config that it found and why it is using that one. -// This can also output the fltk-config version. -// TODO: add a bunch of sensible sample shell commands -// TODO: when this new feature is used for the very first time, import two or three samples as initial user setting -// TODO: make the settings dialog resizable -// TODO: make g_shell_config static, not a pointer, but don't load anything in batch mode - -// FEATURE: fld::Tool_Store icons are currently redundant with @file and @save and could be improved -// FEATURE: hostname, username, getenv support? -// FEATURE: add the files ./fluid.prefs and ./fluid.user.prefs as tool locations -// FEATURE: interpret compiler output, for example: clang, and highlight errors and warnings -// `.../shell_command.cxx:71:2: error: test` -// `71 | #error test` -// `clang++: error: no such file or directory: '.../shell_command.o'` -// would make the error message clickable in the shell window and could select the widget, -// open the matching editor in the widget panel, and highlight the line in SourceView. - -/* - Some ideas: - - default shell is in $SHELL on linux and macOS - - On macOS, we can write Apple Scripts: - - #!/usr/bin/env osascript - say "@BASENAME@" - - osascript <<EOD - say "spark" - EOD - - osascript <<EOD - tell application "Xcode" - build workspace document 1 - end tell - EOD - - powershell -c "$wshell = New-Object -ComObject wscript.shell; $wshell.SendKeys('^{ESCAPE}') - */ - #include "app/shell_command.h" #include "Fluid.h" @@ -109,17 +28,15 @@ #include <FL/fl_string_functions.h> #include <errno.h> +#include <string.h> -using namespace fld; - -static std::string fltk_config_cmd; static Fl_Process s_proc; /** See if shell command is running (public) */ -bool shell_command_running() { - return s_proc.desc() ? true : false; +int shell_command_running() { + return s_proc.desc() ? 1 : 0; } /** \class Fl_Process @@ -130,13 +47,13 @@ bool shell_command_running() { Create a process manager */ Fl_Process::Fl_Process() { + _fpt = 0; } /** Destroy the project manager. */ Fl_Process::~Fl_Process() { - // TODO: check what we need to do if a task is still running if (_fpt) close(); } @@ -150,13 +67,13 @@ Fl_Process::~Fl_Process() { FILE * Fl_Process::popen(const char *cmd, const char *mode) { #if defined(_WIN32) && !defined(__CYGWIN__) // PRECONDITIONS - if (!mode || !*mode || (*mode!='r' && *mode!='w') ) return nullptr; + if (!mode || !*mode || (*mode!='r' && *mode!='w') ) return 0; if (_fpt) close(); // close first before reuse ptmode = *mode; pin[0] = pin[1] = pout[0] = pout[1] = perr[0] = perr[1] = INVALID_HANDLE_VALUE; // stderr to stdout wanted ? - int fusion = (strstr(cmd,"2>&1") !=nullptr); + int fusion = (strstr(cmd,"2>&1") != 0); // Create windows pipes if (!createPipe(pin) || !createPipe(pout) || (!fusion && !createPipe(perr) ) ) @@ -170,8 +87,8 @@ FILE * Fl_Process::popen(const char *cmd, const char *mode) { si.hStdOutput = pout[1]; si.hStdError = fusion ? pout[1] : perr [1]; - if ( CreateProcess(nullptr, (LPTSTR) cmd,nullptr,nullptr,TRUE, - DETACHED_PROCESS,nullptr,nullptr, &si, &pi)) { + if ( CreateProcess(0, (LPTSTR) cmd, 0, 0, TRUE, + DETACHED_PROCESS, 0, 0, &si, &pi)) { // don't need theses handles inherited by child process: clean_close(pin[0]); clean_close(pout[1]); clean_close(perr[1]); HANDLE & h = *mode == 'r' ? pout[0] : pin[1]; @@ -198,13 +115,13 @@ int Fl_Process::close() { clean_close(perr[0]); clean_close(pin[1]); clean_close(pout[0]); - _fpt = nullptr; + _fpt = 0; return 0; } return -1; #else int ret = ::pclose(_fpt); - _fpt=nullptr; + _fpt = 0; return ret; #endif } @@ -223,16 +140,14 @@ FILE *Fl_Process::desc() const { \param[out] line buffer to receive the line \param[in] s size of the provided buffer - \return nullptr if an error occurred, otherwise a pointer to the string + \return 0 if an error occurred, otherwise a pointer to the string */ char *Fl_Process::get_line(char * line, size_t s) const { - return _fpt ? fgets(line, (int)s, _fpt) : nullptr; + return _fpt ? fgets(line, (int)s, _fpt) : 0; } // returns fileno(FILE*): // (file must be open, i.e. _fpt must be non-null) -// *FIXME* we should find a better solution for the 'fileno' issue -// non null if file is open int Fl_Process::get_fileno() const { #ifdef _MSC_VER return _fileno(_fpt); // suppress MSVC warning @@ -243,19 +158,19 @@ int Fl_Process::get_fileno() const { #if defined(_WIN32) && !defined(__CYGWIN__) -bool Fl_Process::createPipe(HANDLE * h, BOOL bInheritHnd) { +int Fl_Process::createPipe(HANDLE * h, BOOL bInheritHnd) { SECURITY_ATTRIBUTES sa; sa.nLength = sizeof(sa); - sa.lpSecurityDescriptor = nullptr; + sa.lpSecurityDescriptor = 0; sa.bInheritHandle = bInheritHnd; - return CreatePipe (&h[0],&h[1],&sa,0) ? true : false; + return CreatePipe (&h[0],&h[1],&sa,0) ? 1 : 0; } FILE *Fl_Process::freeHandles() { clean_close(pin[0]); clean_close(pin[1]); clean_close(pout[0]); clean_close(pout[1]); clean_close(perr[0]); clean_close(perr[1]); - return nullptr; // convenient for error management + return 0; // convenient for error management } void Fl_Process::clean_close(HANDLE& h) { @@ -270,24 +185,23 @@ void Fl_Process::clean_close(HANDLE& h) { Prepare FLUID for running a shell command according to the command flags. \param[in] flags set various flags to save the project, code, and string before running the command - \return false if the previous command is still running + \return 0 if the previous command is still running */ -static bool prepare_shell_command(int flags) { -// settings_window->hide(); +static int prepare_shell_command(int flags) { if (s_proc.desc()) { fl_alert("Previous shell command still running!"); - return false; + return 0; } if (flags & Fd_Shell_Command::SAVE_PROJECT) { - Fluid.save_project_file(nullptr); + Fluid.save_project_file(0); } if (flags & Fd_Shell_Command::SAVE_SOURCECODE) { - Fluid.write_code_files(true); + Fluid.write_code_files(1); } if (flags & Fd_Shell_Command::SAVE_STRINGS) { Fluid.proj.write_strings(); } - return true; + return 1; } /** @@ -312,7 +226,7 @@ void shell_timer_cb(void*) { void shell_pipe_cb(FL_SOCKET, void*) { char line[1024]=""; // Line from command output... - if (s_proc.get_line(line, sizeof(line)) != nullptr) { + if (s_proc.get_line(line, sizeof(line)) != 0) { // Add the line to the output list... shell_run_terminal->append(line); } else { @@ -324,38 +238,77 @@ void shell_pipe_cb(FL_SOCKET, void*) { } } -/** Find the script `fltk-config` that most closely relates to this version of FLUID. - This is not implemented yet. +/** + Replace all occurrences of a macro with the given content. + + \param[inout] cmd buffer holding the command (must have enough space) + \param[in] cmd_size size of the command buffer + \param[in] macro the macro string to replace (e.g., "@BASENAME@") + \param[in] content the replacement string */ -//static void find_fltk_config() { -// -//} +static void expand_macro(char *cmd, int cmd_size, const char *macro, const char *content) { + char *pos; + int macro_len; + int content_len; + int tail_len; + + if (!cmd || !macro || !content) return; + macro_len = (int)strlen(macro); + content_len = (int)strlen(content); -static void expand_macro(std::string &cmd, const std::string ¯o, const std::string &content) { - for (int i=0;;) { - i = (int)cmd.find(macro, i); - if (i==(int)std::string::npos) break; - cmd.replace(i, macro.size(), content); + while ((pos = strstr(cmd, macro)) != 0) { + tail_len = (int)strlen(pos + macro_len); + if ((pos - cmd) + content_len + tail_len >= cmd_size - 1) { + // Not enough space, truncate + break; + } + memmove(pos + content_len, pos + macro_len, tail_len + 1); + memcpy(pos, content, content_len); } } -static void expand_macros(std::string &cmd) { - expand_macro(cmd, "@BASENAME@", Fluid.proj.basename()); - expand_macro(cmd, "@PROJECTFILE_PATH@", Fluid.proj.projectfile_path()); - expand_macro(cmd, "@PROJECTFILE_NAME@", Fluid.proj.projectfile_name()); - expand_macro(cmd, "@CODEFILE_PATH@", Fluid.proj.codefile_path()); - expand_macro(cmd, "@CODEFILE_NAME@", Fluid.proj.codefile_name()); - expand_macro(cmd, "@HEADERFILE_PATH@", Fluid.proj.headerfile_path()); - expand_macro(cmd, "@HEADERFILE_NAME@", Fluid.proj.headerfile_name()); - expand_macro(cmd, "@TEXTFILE_PATH@", Fluid.proj.stringsfile_path()); - expand_macro(cmd, "@TEXTFILE_NAME@", Fluid.proj.stringsfile_name()); -// TODO: implement finding the script `fltk-config` for all platforms -// if (cmd.find("@FLTK_CONFIG@") != std::string::npos) { -// find_fltk_config(); -// expand_macro(cmd, "@FLTK_CONFIG@", fltk_config_cmd.c_str()); -// } - if (cmd.find("@TMPDIR@") != std::string::npos) - expand_macro(cmd, "@TMPDIR@", Fluid.get_tmpdir()); +/** + Expand all known macros in the command string. + + \param[inout] cmd buffer holding the command + \param[in] cmd_size size of the command buffer + */ +static void expand_macros(char *cmd, int cmd_size) { + char buf[FL_PATH_MAX]; + + Fluid.proj.basename(buf, FL_PATH_MAX); + expand_macro(cmd, cmd_size, "@BASENAME@", buf); + + Fluid.proj.projectfile_path(buf, FL_PATH_MAX); + expand_macro(cmd, cmd_size, "@PROJECTFILE_PATH@", buf); + + { + const char *name = Fluid.proj.projectfile_name(); + expand_macro(cmd, cmd_size, "@PROJECTFILE_NAME@", name ? name : ""); + } + + Fluid.proj.codefile_path(buf, FL_PATH_MAX); + expand_macro(cmd, cmd_size, "@CODEFILE_PATH@", buf); + + Fluid.proj.codefile_name(buf, FL_PATH_MAX); + expand_macro(cmd, cmd_size, "@CODEFILE_NAME@", buf); + + Fluid.proj.headerfile_path(buf, FL_PATH_MAX); + expand_macro(cmd, cmd_size, "@HEADERFILE_PATH@", buf); + + Fluid.proj.headerfile_name(buf, FL_PATH_MAX); + expand_macro(cmd, cmd_size, "@HEADERFILE_NAME@", buf); + + Fluid.proj.stringsfile_path(buf, FL_PATH_MAX); + expand_macro(cmd, cmd_size, "@TEXTFILE_PATH@", buf); + + Fluid.proj.stringsfile_name(buf, FL_PATH_MAX); + expand_macro(cmd, cmd_size, "@TEXTFILE_NAME@", buf); + + if (strstr(cmd, "@TMPDIR@") != 0) { + const char *tmpdir = Fluid.get_tmpdir(); + expand_macro(cmd, cmd_size, "@TMPDIR@", tmpdir ? tmpdir : ""); + } } /** @@ -374,22 +327,26 @@ void show_terminal_window() { shell_run_window->show(); } +#define SHELL_CMD_BUF_SIZE 4096 + /** Prepare for and run a shell command. \param[in] cmd the command that is sent to `/bin/sh -c ...` or `cmd.exe` on Windows machines \param[in] flags various flags in preparation of the command */ -void run_shell_command(const std::string &cmd, int flags) { - if (cmd.empty()) { +void run_shell_command(const char *cmd, int flags) { + char expanded_cmd[SHELL_CMD_BUF_SIZE]; + + if (!cmd || !cmd[0]) { fl_alert("No shell command entered!"); return; } if (!prepare_shell_command(flags)) return; - std::string expanded_cmd = cmd; - expand_macros(expanded_cmd); + strlcpy(expanded_cmd, cmd, sizeof(expanded_cmd)); + expand_macros(expanded_cmd, sizeof(expanded_cmd)); if ( ((flags & Fd_Shell_Command::DONT_SHOW_TERMINAL) == 0) && (!shell_run_window->visible())) @@ -403,10 +360,10 @@ void run_shell_command(const std::string &cmd, int flags) { if (flags & Fd_Shell_Command::CLEAR_HISTORY) shell_run_terminal->printf("\033[3J"); shell_run_terminal->scrollbar->value(0); - shell_run_terminal->printf("\033[0;32m%s\033[0m\n", expanded_cmd.c_str()); - shell_run_window->label(expanded_cmd.c_str()); + shell_run_terminal->printf("\033[0;32m%s\033[0m\n", expanded_cmd); + shell_run_window->label(expanded_cmd); - if (s_proc.popen((char *)expanded_cmd.c_str()) == nullptr) { + if (s_proc.popen(expanded_cmd, "r") == 0) { shell_run_terminal->printf("\033[1;31mUnable to run shell command: %s\033[0m\n", strerror(errno)); shell_run_window->label("FLUID Shell"); @@ -421,15 +378,24 @@ void run_shell_command(const std::string &cmd, int flags) { Fl::add_fd(s_proc.get_fileno(), shell_pipe_cb); } +// Helper to duplicate a string safely +static char *dup_str(const char *s) { + return s ? strdup(s) : 0; +} + /** Create an empty shell command structure. */ Fd_Shell_Command::Fd_Shell_Command() -: shortcut(0), +: name(0), + label(0), + shortcut(0), storage(FLD_TOOL_STORE_USER), condition(0), + condition_data(0), + command(0), flags(0), - shell_menu_item_(nullptr) + shell_menu_item_(0) { } @@ -439,75 +405,94 @@ Fd_Shell_Command::Fd_Shell_Command() \param[in] rhs copy from this prototype */ Fd_Shell_Command::Fd_Shell_Command(const Fd_Shell_Command *rhs) -: name(rhs->name), - label(rhs->label), +: name(dup_str(rhs->name)), + label(dup_str(rhs->label)), shortcut(rhs->shortcut), storage(rhs->storage), condition(rhs->condition), - condition_data(rhs->condition_data), - command(rhs->command), + condition_data(dup_str(rhs->condition_data)), + command(dup_str(rhs->command)), flags(rhs->flags), - shell_menu_item_(nullptr) + shell_menu_item_(0) { } /** Create a default storage for a shell command and how it is accessible in FLUID. - \param[in] name is used as a stand-in for the command name and label + \param[in] in_name is used as a stand-in for the command name and label */ -Fd_Shell_Command::Fd_Shell_Command(const std::string &in_name) -: name(in_name), - label(in_name), +Fd_Shell_Command::Fd_Shell_Command(const char *in_name) +: name(dup_str(in_name)), + label(dup_str(in_name)), shortcut(0), storage(FLD_TOOL_STORE_USER), condition(Fd_Shell_Command::ALWAYS), - command("echo \"Hello, FLUID!\""), + condition_data(0), + command(strdup("echo \"Hello, FLUID!\"")), flags(Fd_Shell_Command::SAVE_PROJECT|Fd_Shell_Command::SAVE_SOURCECODE), - shell_menu_item_(nullptr) + shell_menu_item_(0) { } /** Create a storage for a shell command and how it is accessible in FLUID. - - \param[in] in_name name of this command in the command list in the settings panel - \param[in] in_label label text in the main pulldown menu - \param[in] in_shortcut a keyboard shortcut that will also appear in the main menu - \param[in] in_storage storage location for this command - \param[in] in_condition commands can be hidden for certain platforms by setting a condition - \param[in] in_condition_data more details for future conditions, i.e. per user, per host, etc. - \param[in] in_command the shell command that we want to run - \param[in] in_flags some flags to tell FLUID to save the project, code, or strings before running the command - */ -Fd_Shell_Command::Fd_Shell_Command(const std::string &in_name, - const std::string &in_label, + */ +Fd_Shell_Command::Fd_Shell_Command(const char *in_name, + const char *in_label, Fl_Shortcut in_shortcut, fld::Tool_Store in_storage, int in_condition, - const std::string &in_condition_data, - const std::string &in_command, + const char *in_condition_data, + const char *in_command, int in_flags) -: name(in_name), - label(in_label), +: name(dup_str(in_name)), + label(dup_str(in_label)), shortcut(in_shortcut), storage(in_storage), condition(in_condition), - condition_data(in_condition_data), - command(in_command), + condition_data(dup_str(in_condition_data)), + command(dup_str(in_command)), flags(in_flags), - shell_menu_item_(nullptr) + shell_menu_item_(0) { } /** - Run this command now. + Destructor - free all allocated strings. + */ +Fd_Shell_Command::~Fd_Shell_Command() { + if (name) free(name); + if (label) free(label); + if (condition_data) free(condition_data); + if (command) free(command); +} + +void Fd_Shell_Command::set_name(const char *s) { + if (name) free(name); + name = dup_str(s); +} + +void Fd_Shell_Command::set_label(const char *s) { + if (label) free(label); + label = dup_str(s); +} + +void Fd_Shell_Command::set_condition_data(const char *s) { + if (condition_data) free(condition_data); + condition_data = dup_str(s); +} + +void Fd_Shell_Command::set_command(const char *s) { + if (command) free(command); + command = dup_str(s); +} - Will open the Shell Panel and execute the command if no other command is - currently running. +/** + Run this command now. */ void Fd_Shell_Command::run() { - if (!command.empty()) + if (command && command[0]) run_shell_command(command, flags); } @@ -516,9 +501,9 @@ void Fd_Shell_Command::run() { */ void Fd_Shell_Command::update_shell_menu() { if (shell_menu_item_) { - const char *old_label = shell_menu_item_->label(); // can be nullptr - const char *new_label = label.c_str(); // never nullptr - if (!old_label || (old_label && strcmp(old_label, new_label))) { + const char *old_label = shell_menu_item_->label(); // can be 0 + const char *new_label = label ? label : ""; + if (!old_label || strcmp(old_label, new_label)) { if (old_label) ::free((void*)old_label); shell_menu_item_->label(fl_strdup(new_label)); } @@ -529,66 +514,75 @@ void Fd_Shell_Command::update_shell_menu() { /** Check if the set condition is met. - \return true if this command appears in the main menu + \return 1 if this command appears in the main menu */ -bool Fd_Shell_Command::is_active() { +int Fd_Shell_Command::is_active() { switch (condition) { - case ALWAYS: return true; - case NEVER: return false; + case ALWAYS: return 1; + case NEVER: return 0; #ifdef _WIN32 - case MAC_ONLY: return false; - case UX_ONLY: return false; - case WIN_ONLY: return true; - case MAC_AND_UX_ONLY: return false; + case MAC_ONLY: return 0; + case UX_ONLY: return 0; + case WIN_ONLY: return 1; + case MAC_AND_UX_ONLY: return 0; #elif defined(__APPLE__) - case MAC_ONLY: return true; - case UX_ONLY: return false; - case WIN_ONLY: return false; - case MAC_AND_UX_ONLY: return true; + case MAC_ONLY: return 1; + case UX_ONLY: return 0; + case WIN_ONLY: return 0; + case MAC_AND_UX_ONLY: return 1; #else - case MAC_ONLY: return false; - case UX_ONLY: return true; - case WIN_ONLY: return false; - case MAC_AND_UX_ONLY: return true; + case MAC_ONLY: return 0; + case UX_ONLY: return 1; + case WIN_ONLY: return 0; + case MAC_AND_UX_ONLY: return 1; #endif - case USER_ONLY: return false; // TODO: get user name - case HOST_ONLY: return false; // TODO: get host name + case USER_ONLY: return 0; // TODO: get user name + case HOST_ONLY: return 0; // TODO: get host name case ENV_ONLY: { - const char *value = fl_getenv(condition_data.c_str()); - if (value && *value) return true; - return false; + const char *value = condition_data ? fl_getenv(condition_data) : 0; + if (value && *value) return 1; + return 0; } } - return false; + return 0; } void Fd_Shell_Command::read(Fl_Preferences &prefs) { int tmp; char *str_ptr = 0; prefs.get("name", str_ptr, "<unnamed>"); - if (str_ptr) { name = str_ptr; free(str_ptr); str_ptr = 0; } + set_name(str_ptr); + if (str_ptr) { free(str_ptr); str_ptr = 0; } + prefs.get("label", str_ptr, "<no label>"); - if (str_ptr) { label = str_ptr; free(str_ptr); str_ptr = 0; } + set_label(str_ptr); + if (str_ptr) { free(str_ptr); str_ptr = 0; } + prefs.get("shortcut", tmp, 0); shortcut = (Fl_Shortcut)tmp; prefs.get("storage", tmp, -1); if (tmp != -1) storage = (fld::Tool_Store)tmp; prefs.get("condition", condition, ALWAYS); + prefs.get("condition_data", str_ptr, ""); - if (str_ptr) { condition_data = str_ptr; free(str_ptr); str_ptr = 0; } + set_condition_data(str_ptr); + if (str_ptr) { free(str_ptr); str_ptr = 0; } + prefs.get("command", str_ptr, ""); - if (str_ptr) { command = str_ptr; free(str_ptr); str_ptr = 0; } + set_command(str_ptr); + if (str_ptr) { free(str_ptr); str_ptr = 0; } + prefs.get("flags", flags, 0); } -void Fd_Shell_Command::write(Fl_Preferences &prefs, bool save_location) { - prefs.set("name", name.c_str()); - prefs.set("label", label.c_str()); +void Fd_Shell_Command::write(Fl_Preferences &prefs, int save_location) { + prefs.set("name", name ? name : ""); + prefs.set("label", label ? label : ""); if (shortcut != 0) prefs.set("shortcut", (int)shortcut); if (save_location) prefs.set("storage", (int)storage); if (condition != ALWAYS) prefs.set("condition", condition); - if (!condition_data.empty()) prefs.set("condition_data", condition_data.c_str()); - if (!command.empty()) prefs.set("command", command.c_str()); + if (condition_data && condition_data[0]) prefs.set("condition_data", condition_data); + if (command && command[0]) prefs.set("command", command); if (flags != 0) prefs.set("flags", flags); } @@ -600,17 +594,17 @@ void Fd_Shell_Command::read(class fld::io::Project_Reader *in) { c = in->read_word(1); if (strcmp(c, "}")==0) break; // end of command list else if (strcmp(c, "name")==0) - name = in->read_word(); + set_name(in->read_word()); else if (strcmp(c, "label")==0) - label = in->read_word(); + set_label(in->read_word()); else if (strcmp(c, "shortcut")==0) shortcut = in->read_int(); else if (strcmp(c, "condition")==0) condition = in->read_int(); else if (strcmp(c, "condition_data")==0) - condition_data = in->read_word(); + set_condition_data(in->read_word()); else if (strcmp(c, "command")==0) - command = in->read_word(); + set_command(in->read_word()); else if (strcmp(c, "flags")==0) flags = in->read_int(); else @@ -620,14 +614,14 @@ void Fd_Shell_Command::read(class fld::io::Project_Reader *in) { void Fd_Shell_Command::write(class fld::io::Project_Writer *out) { out->write_string("\n command {"); - out->write_string("\n name "); out->write_word(name); - out->write_string("\n label "); out->write_word(label); + out->write_string("\n name "); out->write_word(name ? name : ""); + out->write_string("\n label "); out->write_word(label ? label : ""); if (shortcut) out->write_string("\n shortcut %d", shortcut); if (condition) out->write_string("\n condition %d", condition); - if (!condition_data.empty()) { + if (condition_data && condition_data[0]) { out->write_string("\n condition_data "); out->write_word(condition_data); } - if (!command.empty()) { + if (command && command[0]) { out->write_string("\n command "); out->write_word(command); } if (flags) out->write_string("\n flags %d", flags); @@ -639,6 +633,10 @@ void Fd_Shell_Command::write(class fld::io::Project_Writer *out) { Manage a list of shell commands and their parameters. */ Fd_Shell_Command_List::Fd_Shell_Command_List() +: list(0), + list_size(0), + list_capacity(0), + shell_menu_(0) { } @@ -664,13 +662,14 @@ Fd_Shell_Command *Fd_Shell_Command_List::at(int index) const { */ void Fd_Shell_Command_List::clear() { if (list) { - for (int i=0; i<list_size; i++) { + int i; + for (i=0; i<list_size; i++) { delete list[i]; } ::free(list); list_size = 0; list_capacity = 0; - list = nullptr; + list = 0; } } @@ -678,7 +677,8 @@ void Fd_Shell_Command_List::clear() { remove all shell commands of the given storage location from the list. */ void Fd_Shell_Command_List::clear(fld::Tool_Store storage) { - for (int i=list_size-1; i>=0; i--) { + int i; + for (i=list_size-1; i>=0; i--) { if (list[i]->storage == storage) { remove(i); } @@ -689,6 +689,7 @@ void Fd_Shell_Command_List::clear(fld::Tool_Store storage) { Read shell configuration from a preferences group. */ void Fd_Shell_Command_List::read(Fl_Preferences &prefs, fld::Tool_Store storage) { + int i, n; // import the old shell commands from previous user settings if (&Fluid.preferences == &prefs) { int version; @@ -697,12 +698,12 @@ void Fd_Shell_Command_List::read(Fl_Preferences &prefs, fld::Tool_Store storage) int save_fl, save_code, save_strings; Fd_Shell_Command *cmd = new Fd_Shell_Command(); cmd->storage = FLD_TOOL_STORE_USER; - cmd->name = "Sample Shell Command"; - cmd->label = "Sample Shell Command"; + cmd->set_name("Sample Shell Command"); + cmd->set_label("Sample Shell Command"); cmd->shortcut = FL_ALT+'g'; char *cmd_str = 0; Fluid.preferences.get("shell_command", cmd_str, "echo \"Sample Shell Command\""); - if (cmd_str) { cmd->command = cmd_str; free(cmd_str); } + if (cmd_str) { cmd->set_command(cmd_str); free(cmd_str); } Fluid.preferences.get("shell_savefl", save_fl, 1); Fluid.preferences.get("shell_writecode", save_code, 1); Fluid.preferences.get("shell_writemsgs", save_strings, 0); @@ -715,8 +716,8 @@ void Fd_Shell_Command_List::read(Fl_Preferences &prefs, fld::Tool_Store storage) prefs.set("shell_commands_version", version); } Fl_Preferences shell_commands(prefs, "shell_commands"); - int n = shell_commands.groups(); - for (int i=0; i<n; i++) { + n = shell_commands.groups(); + for (i=0; i<n; i++) { Fl_Preferences cmd_prefs(shell_commands, Fl_Preferences::Name(i)); Fd_Shell_Command *cmd = new Fd_Shell_Command(); cmd->storage = FLD_TOOL_STORE_USER; @@ -729,13 +730,14 @@ void Fd_Shell_Command_List::read(Fl_Preferences &prefs, fld::Tool_Store storage) Write shell configuration to a preferences group. */ void Fd_Shell_Command_List::write(Fl_Preferences &prefs, fld::Tool_Store storage) { + int i, index; Fl_Preferences shell_commands(prefs, "shell_commands"); shell_commands.delete_all_groups(); - int index = 0; - for (int i=0; i<list_size; i++) { + index = 0; + for (i=0; i<list_size; i++) { if (list[i]->storage == FLD_TOOL_STORE_USER) { Fl_Preferences cmd(shell_commands, Fl_Preferences::Name(index++)); - list[i]->write(cmd); + list[i]->write(cmd, 0); } } } @@ -764,14 +766,14 @@ void Fd_Shell_Command_List::read(fld::io::Project_Reader *in) { Write shell configuration to a project file. */ void Fd_Shell_Command_List::write(fld::io::Project_Writer *out) { - int n_in_project_file = 0; - for (int i=0; i<list_size; i++) { + int i, n_in_project_file = 0; + for (i=0; i<list_size; i++) { if (list[i]->storage == FLD_TOOL_STORE_PROJECT) n_in_project_file++; } if (n_in_project_file > 0) { out->write_string("\nshell_commands {"); - for (int i=0; i<list_size; i++) { + for (i=0; i<list_size; i++) { if (list[i]->storage == FLD_TOOL_STORE_PROJECT) list[i]->write(out); } @@ -842,12 +844,14 @@ void menu_shell_customize_cb(Fl_Widget*, void*) { Rebuild the entire shell submenu from scratch and replace the old menu. */ void Fd_Shell_Command_List::rebuild_shell_menu() { - static Fl_Menu_Item *shell_submenu = nullptr; + static Fl_Menu_Item *shell_submenu = 0; + int i, j, num_active_items; + if (!shell_submenu) shell_submenu = (Fl_Menu_Item*)Fluid.main_menubar->find_item(menu_marker); - int i, j, num_active_items = 0; // count the active commands + num_active_items = 0; for (i=0; i<list_size; i++) { if (list[i]->is_active()) num_active_items++; } @@ -875,9 +879,9 @@ void Fd_Shell_Command_List::rebuild_shell_menu() { // free all resources from the old menu if (mi_old && (mi_old != default_menu)) { for (i=0; ; i++) { - const char *label = mi_old[i].label(); - if (!label) break; - ::free((void*)label); + const char *lbl = mi_old[i].label(); + if (!lbl) break; + ::free((void*)lbl); } ::free(mi_old); } @@ -896,7 +900,7 @@ void Fd_Shell_Command_List::update_settings_dialog() { */ Fl_Menu_Item Fd_Shell_Command_List::default_menu[] = { { "Customize...", FL_ALT+'x', menu_shell_customize_cb }, - { nullptr } + { 0 } }; /** @@ -908,12 +912,11 @@ void Fd_Shell_Command_List::menu_marker(Fl_Widget*, void*) { /** Export all selected shell commands to an external file. - - Verify that g_shell_config and w_settings_shell_list are not nullptr. Open a - file chooser and export all items that are selected in w_settings_shell_list - into an external file. */ void Fd_Shell_Command_List::export_selected() { + char path[FL_PATH_MAX]; + char preset[FL_PATH_MAX]; + if (!g_shell_config || (g_shell_config->list_size == 0)) return; if (!w_settings_shell_list) return; @@ -921,28 +924,37 @@ void Fd_Shell_Command_List::export_selected() { dialog.title("Export selected shell commands:"); dialog.type(Fl_Native_File_Chooser::BROWSE_SAVE_FILE); dialog.filter("FLUID Files\t*.flcmd\n"); - dialog.directory(Fluid.proj.projectfile_path().c_str()); - dialog.preset_file((Fluid.proj.basename() + ".flcmd").c_str()); + + Fluid.proj.projectfile_path(path, FL_PATH_MAX); + dialog.directory(path); + + Fluid.proj.basename(preset, FL_PATH_MAX); + strlcat(preset, ".flcmd", FL_PATH_MAX); + dialog.preset_file(preset); + if (dialog.show() != 0) return; - Fl_Preferences file(dialog.filename(), "flcmd.fluid.fltk.org", nullptr, (Fl_Preferences::Root)(Fl_Preferences::C_LOCALE|Fl_Preferences::CLEAR)); + Fl_Preferences file(dialog.filename(), "flcmd.fluid.fltk.org", 0, (Fl_Preferences::Root)(Fl_Preferences::C_LOCALE|Fl_Preferences::CLEAR)); Fl_Preferences shell_commands(file, "shell_commands"); - int i, index = 0, n = w_settings_shell_list->size(); + int i, index, n; + index = 0; + n = w_settings_shell_list->size(); for (i = 0; i < n; i++) { if (w_settings_shell_list->selected(i+1)) { Fl_Preferences cmd(shell_commands, Fl_Preferences::Name(index++)); - g_shell_config->list[i]->write(cmd, true); + g_shell_config->list[i]->write(cmd, 1); } } } /** Import shell commands from an external file and add them to the list. - - Verify that g_shell_config and w_settings_shell_list are not nullptr. Open a - file chooser and import all items. */ void Fd_Shell_Command_List::import_from_file() { + char path[FL_PATH_MAX]; + char preset[FL_PATH_MAX]; + int i, n; + if (!g_shell_config || (g_shell_config->list_size == 0)) return; if (!w_settings_shell_list) return; @@ -950,13 +962,19 @@ void Fd_Shell_Command_List::import_from_file() { dialog.title("Import shell commands:"); dialog.type(Fl_Native_File_Chooser::BROWSE_FILE); dialog.filter("FLUID Files\t*.flcmd\n"); - dialog.directory(Fluid.proj.projectfile_path().c_str()); - dialog.preset_file((Fluid.proj.basename() + ".flcmd").c_str()); + + Fluid.proj.projectfile_path(path, FL_PATH_MAX); + dialog.directory(path); + + Fluid.proj.basename(preset, FL_PATH_MAX); + strlcat(preset, ".flcmd", FL_PATH_MAX); + dialog.preset_file(preset); + if (dialog.show() != 0) return; - Fl_Preferences file(dialog.filename(), "flcmd.fluid.fltk.org", nullptr, Fl_Preferences::C_LOCALE); + Fl_Preferences file(dialog.filename(), "flcmd.fluid.fltk.org", 0, Fl_Preferences::C_LOCALE); Fl_Preferences shell_commands(file, "shell_commands"); - int i, n = shell_commands.groups(); + n = shell_commands.groups(); for (i = 0; i < n; i++) { Fl_Preferences cmd_prefs(shell_commands, Fl_Preferences::Name(i)); Fd_Shell_Command *cmd = new Fd_Shell_Command(); @@ -973,5 +991,4 @@ void Fd_Shell_Command_List::import_from_file() { /** A pointer to the list of shell commands if we are not in batch mode. */ -Fd_Shell_Command_List *g_shell_config = nullptr; - +Fd_Shell_Command_List *g_shell_config = 0; |
