diff --git a/src/include/lib3270++.h b/src/include/lib3270++.h index e42a384..27f4447 100644 --- a/src/include/lib3270++.h +++ b/src/include/lib3270++.h @@ -262,6 +262,11 @@ /// @brief Insert event listener. void insert(Event::Type type, std::function listener); + // Misc + + /// @brief Execute action by name. + virtual Session & action(const char *action_name) = 0; + }; /// @brief TN3270 Host diff --git a/src/lib3270++/ipc/session.cc b/src/lib3270++/ipc/session.cc index 1b74f11..380602a 100644 --- a/src/lib3270++/ipc/session.cc +++ b/src/lib3270++/ipc/session.cc @@ -224,21 +224,11 @@ "eraseinput" }; - int32_t rc; - if( ((size_t) action) > (sizeof(actions)/sizeof(actions[0]))) { throw std::system_error(EINVAL, std::system_category()); } - Request(*this,actions[(size_t) action]) - .call() - .pop(rc); - - if(rc) { - throw std::system_error((int) rc, std::system_category()); - } - - return *this; + return this->action(actions[action]); } @@ -353,6 +343,23 @@ } + /// @brief Execute action by name. + TN3270::Session & IPC::Session::action(const char *action_name) { + + int32_t rc; + + Request(*this,"action") + .push(action_name) + .call() + .pop(rc); + + if(rc) { + throw std::system_error((int) rc, std::system_category()); + } + + return *this; + } + } diff --git a/src/lib3270++/local/session.cc b/src/lib3270++/local/session.cc index df3ad1b..be73527 100644 --- a/src/lib3270++/local/session.cc +++ b/src/lib3270++/local/session.cc @@ -253,29 +253,27 @@ } TN3270::Session & Local::Session::push(const Action action) { - std::lock_guard lock(sync); - switch(action) { - case ENTER: - lib3270_enter(hSession); - break; + typedef int (*ActionCallback)(H3270 *); - case ERASE: - lib3270_erase(hSession); - break; + static const ActionCallback actions[] = { + lib3270_enter, + lib3270_erase, + lib3270_eraseeof, + lib3270_eraseeol, + lib3270_eraseinput + }; - case ERASE_EOF: - lib3270_eraseeof(hSession); - break; + if( ((size_t) action) > (sizeof(actions)/sizeof(actions[0]))) { + throw std::system_error(EINVAL, std::system_category()); + } - case ERASE_EOL: - lib3270_eraseeol(hSession); - break; + std::lock_guard lock(sync); - case ERASE_INPUT: - lib3270_eraseinput(hSession); - break; + int rc = actions[(size_t) action](hSession); + if(rc) { + throw std::system_error(errno, std::system_category()); } return *this; @@ -376,6 +374,15 @@ return lib3270_get_revision(); } + /// @brief Execute action by name. + TN3270::Session & Local::Session::action(const char *action_name) { + + if(lib3270_action(hSession,action_name)) { + throw std::system_error(errno, std::system_category()); + } + + return *this; + } } diff --git a/src/lib3270++/private.h b/src/lib3270++/private.h index 69d87c5..1be0d5b 100644 --- a/src/lib3270++/private.h +++ b/src/lib3270++/private.h @@ -209,6 +209,9 @@ TN3270::Session & pop(int row, int col, std::string &text) override; TN3270::Session & pop(std::string &text) override; + /// @brief Execute action by name. + TN3270::Session & action(const char *action_name) override; + }; } @@ -370,6 +373,9 @@ TN3270::Session & pop(int row, int col, std::string &text) override; TN3270::Session & pop(std::string &text) override; + /// @brief Execute action by name. + TN3270::Session & action(const char *action_name) override; + }; } -- libgit2 0.21.2