// ========================================================= //
//                                                           //
//   File      : xcmd.hxx                                    //
//   Purpose   : support to run commands in (x)terminal      //
//                                                           //
//   Coded by Ralf Westram (coder@reallysoft.de) in Sep 25   //
//   http://www.arb-home.de/                                 //
//                                                           //
// ========================================================= //

#ifndef XCMD_HXX
#define XCMD_HXX

#ifndef ARBDB_BASE_H
#include <arbdb_base.h>
#endif
#ifndef AW_BASE_HXX
#include <aw_base.hxx>
#endif

enum XCMD_TYPE {
    // internal (do not use; use public values below):
    _XCMD__ASYNC     = 1,  // run asynchronously (otherwise wait until command finishes).
    _XCMD__WAITKEY   = 2,  // after command always wait for keypress (otherwise only in case of error!).
    _XCMD__ACCESS_DB = 4,  // command may access database.
    _XCMD__HIDDEN    = 8,  // do not run in terminal window (never waits for key)

    // rest is public ----------------------------------------

    // the XCMD_ASYNC_... types do not block the database server, but also do not wait for the tool to finish.
    // Has to be used if window shall remain open with command running continously (e.g. to tail a log).
    XCMD_ASYNC_WAITKEY       = _XCMD__ASYNC | _XCMD__ACCESS_DB | _XCMD__WAITKEY,
    XCMD_ASYNC_WAIT_ON_ERROR = _XCMD__ASYNC | _XCMD__ACCESS_DB,
    XCMD_ASYNC_HIDDEN        = _XCMD__ASYNC | _XCMD__ACCESS_DB | _XCMD__HIDDEN,

    // Important note: the XCMD_SYNC_... types block the database server, if started from there.
    // I.e. they are normally critical esp. in arb_ntree, which normally runs as server.
    // This means: if the started tool tries to access the served database,
    //             it will not be able to do so (effectively causes a deadlock!)
    // Use of XCMD_SYNC_...-modes is not recommended, unless you really know what you are doing!
    XCMD_SYNC_WAITKEY       = _XCMD__WAITKEY,
    XCMD_SYNC_WAIT_ON_ERROR = 0,
    XCMD_SYNC_HIDDEN        = _XCMD__HIDDEN,

    // the XCMD_SERVSYNC_... types do not block the database server, but wait until the command finishes.
    //
    // This is a compromise between the XCMD_SERVSYNC_...- and XCMD_ASYNC_...-modes, which uses the database
    // to synchronise between running command and its caller (arb) w/o blocking communication between them.
    XCMD_SERVSYNC_WAITKEY       = _XCMD__ACCESS_DB | _XCMD__WAITKEY,
    XCMD_SERVSYNC_WAIT_ON_ERROR = _XCMD__ACCESS_DB,
    XCMD_SERVSYNC_HIDDEN        = _XCMD__ACCESS_DB | _XCMD__HIDDEN,
};

class XCmdType {
    XCMD_TYPE  type;
    GBDATA    *gb_main;

public:
    XCmdType(XCMD_TYPE type_, GBDATA *gb_main_)
        : type(type_) , gb_main(gb_main_)
    {}

    XCMD_TYPE get_type() const { return type; }
    GBDATA *get_gb_main() const { return gb_main; }
};

GB_ERROR ARB_system(const char *cmd, XCmdType boundExectype);

// ---------------------------------------- see also ../../WINDOW/aw_system.hxx@SHELL_INTERFACE
// system/shell interface (for GUI apps)
// Notes:
// - AW_window parameter is an unused callback-dummy)

class XCmdType;

void ARB_system_in_console_cb(AW_window*, const char *command, const XCmdType *exectype);

// for direct calls use these:
inline void ARB_system_in_console(const char *command, const XCmdType& exectype) { ARB_system_in_console_cb(NULp, command, &exectype); }


#else
#error xcmd.hxx included twice
#endif // XCMD_HXX
