//********************************************************************************
//* File       : Salmon.hpp                                                      *
//* Author     : Mahlon R. Smith                                                 *
//*              Copyright (c) 2012-2026 Mahlon R. Smith, The Software Samurai   *
//*                  GNU GPL copyright notice below.                             *
//* Date       : 27-May-2026                                                     *
//* Version    : (see AppVersion, below)                                         *
//*                                                                              *
//* Description: Define the Salmon class and support data for the Salmon         *
//*              application.                                                    *
//*                                                                              *
//********************************************************************************
//* Copyright Notice:                                                            *
//* This program is free software: you can redistribute it and/or modify it      *
//* under the terms of the GNU General Public License as published by the Free   *
//* Software Foundation, either version 3 of the License, or (at your option)    *
//* any later version.                                                           *
//*                                                                              *
//* This program is distributed in the hope that it will be useful, but          *
//* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY   *
//* or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License     *
//* for more details.                                                            *
//*                                                                              *
//* You should have received a copy of the GNU General Public License along      *
//* with this program.  If not, see <http://www.gnu.org/licenses/>.              *
//*                                                                              *
//********************************************************************************

#include "GlobalDef.hpp"      // standard library headers and general definitions
#include "gString.hpp"        // string analysis and formatting


//*************************************************************
//* Capture and decode data for the terminal emulator program *
//* and the current shell program.                            *
//* This class has been adapted to the current application by *
//* the addition of a few new data members.                   *
//*************************************************************
class emuData
{
   public:
   static const int EBUFF = 32 ;          // size of member buffers
   ~emuData ( void ) { }                  // destructor
   emuData ( void )  { this->reset () ; } // default constructor
   emuData ( const gString& gsSrc )
   {
      this->decode ( gsSrc ) ;
   }
   int decode ( const gString& gsSrc ) // returns number of tokens decoded
   {
      this->reset () ;
      return ( (gsSrc.gscanf( "%s :%d :%s :%s :%d :%s", 
                              this->emuName, &this->emuPid, this->emuVersion, 
                              this->shName,  &this->shPid,  this->shVersion )) ) ;
   }
   void reset ( void )
   {
      *this->emuName = *this->emuVersion = 
      *this->shName  = *this->shVersion  = '\0' ;
      this->emuPid = this->shPid = 0 ;
      this->wCols = this->wRows = this->xOff = this->yOff = 0 ;
      this->fullScr = this->dfltGeo = this->emuTab = false ;
   }
   //** Public Data Members **
   char emuName[EBUFF] ;
   char emuVersion[EBUFF] ;
   char shName[EBUFF] ;
   char shVersion[EBUFF] ;
   int  emuPid ;
   int  shPid ;
   int  wCols, wRows, xOff, yOff ;
   bool fullScr ;
   bool dfltGeo ;
   bool emuTab ;
} ;

//* Local data *
static const char* const AppVersion = "0.0.05" ;
static const char* const AppYears = "2012-2026" ;
static const char* childApp = "childApp" ;  // name of app that child process invokes


enum SalTest : short
{
   stExecV = ZERO,
   stExecL,
   stExecLP,
   stExecForkL,
   stExecForkVP,
   stExecForkLP,
   stSandbox,
   stHelp,
   stVersion
} ;

enum EmuIndex : short
{
   emuiGnome = ZERO,
   emuiKonsole,
   emuiXTerm,
   emuiUnknown
} ;

//****************************************
//* Define the Salmon application class. *
//****************************************
class Salmon
{
   public:

   Salmon ( int argc, const char* argv[] ) ; // initialization constructor
   ~Salmon ( void )                          // destructor
   { /* Nothing to do at this time. */ }

   //* Most methods are declared "private" so  *
   //* they cannot be called by forces of evil.*
   private:
   void Test_execv ( void ) ;
   void Test_execl ( void ) ;
   void Test_execlp ( void ) ;
   void Test_Fork_execl ( void ) ;
   void Test_Fork_execvp ( void ) ;
   void Test_Fork_execlp ( void ) ;
   void Sandbox ( void ) ;
   bool getBasePath ( const char* arg_zero ) ;
   bool getTempfilePath ( void ) ;
   bool createTempfile ( gString& tmpPath ) ;
   bool deleteTempfile ( const gString& tmpPath ) ;
   EmuIndex DefineChildEmulator ( emuData& emu, gString& gsGeo ) ;
   EmuIndex getEmulator ( gString& gsEmu, const char* tmpSpec ) ;
   void reportVersion ( void ) ;             // report version and copyright info
   void helpMe ( void ) ;                    // display application help

   void reset ( void )                       // initialize all data members
   {
      this->argList = NULL ;
      this->argCnt = ZERO ;
      this->pPid = ZERO ;
      *this->appPath = 
      *this->tfPath  = NULLCHAR ;
   }

   private:
   //* The default constructor is declared "private"  *
   //* so any moron who invokes it will get a warning.*
   Salmon ( void ) { }

   //** All Data Members Defined as "protected" **
   protected:
   const char** argList ;                    // command line arguments
   int argCnt ;                              // command line argument count
   pid_t pPid ;                              // application's process ID
   char appPath[PATH_MAX] ;                  // application base path
   char childFSpec[PATH_MAX] ;               // filespec of child application
   char tfPath[PATH_MAX] ;                   // system temp file directory

} ;