menusys.cpp File Reference

menusys implementation More...

#include <unistd.h>
#include <sys/stat.h>
#include <pthread.h>
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.h>
#include <sys/timeb.h>
#include <sys/types.h>
#include <time.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include "menusys.h"
#include "menusysu.h"

Go to the source code of this file.

Defines

#define THREAD_HANDLE   pthread_t
#define THREAD_HANDLE_INIT(x)
#define MAX_PATH   PATH_MAX
#define StringConstant(name, str)   static const TCHAR name[] = str;

Typedefs

typedef std::map< std::tstring,
long > 
ProcMap
 Map the name of a procedure to the offset of that procedure in the script file.
typedef std::vector< long > ProcStack
 Stack of current procedure calls.
typedef BufferImpl< char > BufferA
typedef BufferImpl< TCHAR > Buffer
typedef std::vector< long > LapArray
typedef std::map< std::tstring,
Timer > 
TimerMap
typedef std::map< std::tstring,
MSYS_ThreadImpl * > 
ThreadMap

Enumerations

enum  DISPLAY { DISPLAY_NONE, DISPLAY_PROMPT, DISPLAY_OUTPUT, DISPLAY_ALL }
enum  PARSE { PARSE_DEFAULT, PARSE_ON, PARSE_OFF }

Functions

static void InitInternal ()
static bool FileExists (const TCHAR *aFilePath)
static bool StringReplacements (MSYS_ThreadImpl *aThread, Buffer &aBuf)
static void OutputToConsole (const TCHAR *aString)
static void OutputToTeeFile (MSYS_ThreadImpl *aThread, const Buffer &aBuf)
static bool Input (MSYS_ThreadImpl *aThread, Buffer &aBuf)
static void Parse (TCHAR *aBuf, TCHAR *&aCommand, TCHAR *&aArgs)
static void ParseArgs (TCHAR *aBuf, bool aUnquote, MSYS_Args &aArgs)
static bool Dispatch (MSYS_ThreadImpl *aThread, TCHAR *aCommand, TCHAR *aArgs, PARSE aParse)
static void DoChdir (MSYS_Thread *thrd, MSYS_Args &aArgs)
static void DoEcho (MSYS_Thread *thrd, MSYS_Args &aArgs)
static void DoExit (MSYS_Thread *thrd, MSYS_Args &aArgs)
static void DoHelp (MSYS_Thread *thrd, MSYS_Args &aArgs)
static void DoLoop (MSYS_Thread *thrd, MSYS_Args &aArgs)
static void DoProc (MSYS_Thread *thrd, MSYS_Args &aArgs)
static void DoPwd (MSYS_Thread *thrd, MSYS_Args &aArgs)
static void DoRead (MSYS_Thread *thrd, MSYS_Args &aArgs)
static void DoSetOpt (MSYS_Thread *thrd, MSYS_Args &aArgs)
static void DoShell (MSYS_Thread *thrd, MSYS_Args &aArgs)
static void DoSleep (MSYS_Thread *thrd, MSYS_Args &aArgs)
static void DoTee (MSYS_Thread *thrd, MSYS_Args &aArgs)
static void DoTimer (MSYS_Thread *thrd, MSYS_Args &aArgs)
static void DoThreads (MSYS_Thread *thrd, MSYS_Args &aArgs)
static void OptConsole (MSYS_Thread *thrd, MSYS_Args &aArgs)
static void OptThreads (MSYS_Thread *thrd, MSYS_Args &aArgs)
static void OptUtf8 (MSYS_Thread *thrd, MSYS_Args &aArgs)
static void * InputLoop (void *thrd)
 Main thread function running the command input and dispatch loop.
bool MSYS_Process (const TCHAR *aInputFile, bool aEnableThreads)
 Start the menu system.
static bool ReplaceString (Buffer &aBuf, const TCHAR *aSearch, const TCHAR *aReplace)
void MSYS_Display (MSYS_Thread *thrd, const TCHAR *aFormat,...)
 Display a string.
void MSYS_DisplayWrapped (MSYS_Thread *thrd, const TCHAR *aText)
 Display a string word-wrapped for 80 column display.
bool MSYS_NeedHelp (MSYS_Thread *thrd, MSYS_Handler fn, MSYS_Args &aArgs, bool aShowHelp)
 Returns if detailed help has been requested in the arguments.
static void HelpExternal (MSYS_Thread *, MSYS_Args &)
static void HelpInternal (MSYS_Thread *, MSYS_Args &)
static void HelpAll (MSYS_Thread *, MSYS_Args &)
static void ThreadCreate (MSYS_ThreadImpl *aThread, TCHAR *aThreadName, TCHAR *aPath)
static void ThreadJoin (MSYS_ThreadImpl *aThread, TCHAR *aThreadName)
static void ThreadStop (MSYS_ThreadImpl *aThread, TCHAR *aThreadName)
void MSYS_DisplayOption (MSYS_Thread *aThread, const TCHAR *aOption, const TCHAR *aValue)
 Display an option to the user in the standard way.
static const TCHAR * GetConsoleType (DISPLAY aType)
static int ParseTime (const TCHAR *aTimeString, struct tm &aTime)
static void TimerTell (MSYS_ThreadImpl *aThread, const TCHAR *aTimer, bool aShowRaw)
void MSYS_ShowHelp (MSYS_Thread *thrd, MSYS_Handler fn)
 Display summary help or detailed command help.

Variables

static const TCHAR OPTION_PREFIX [] = _T("_setopt_")
static const size_t OPTION_PREFIX_LEN = (sizeof(OPTION_PREFIX) / sizeof(TCHAR)) - 1
const TCHAR * MSYS_HEADER = _T("")
const MSYS_DispatchEntry g_rgInternal []
static MSYS_DispatchEntryg_rgDispatch = NULL
static bool g_bEnableThreads = false
static ThreadMap g_mapThreads
static ThreadLock g_lockThreadsMap
static ThreadLock g_lockOutputConsole
static ThreadLock g_lockOutputTeeFile
static MSYS_ThreadImpl * g_pRootThread = NULL
const MSYS_DispatchEntry g_rgInternal []


Detailed Description

menusys implementation

Version:
1.2

Definition in file menusys.cpp.


Define Documentation

#define MAX_PATH   PATH_MAX

Definition at line 23 of file menusys.cpp.

Referenced by DoPwd(), and DoSetOpt().

#define StringConstant ( name,
str   )     static const TCHAR name[] = str;

String constants are a pain when using ICU on platforms that have wchar_t size different to the UChar size (i.e. Mac, Unix). On these platforms we need to construct the constants with some effort. On Windows we can just define them simply.

Definition at line 69 of file menusys.cpp.

#define THREAD_HANDLE   pthread_t

Definition at line 19 of file menusys.cpp.

#define THREAD_HANDLE_INIT (  ) 

Definition at line 20 of file menusys.cpp.


Typedef Documentation

typedef BufferImpl<TCHAR> Buffer

Definition at line 125 of file menusys.cpp.

typedef BufferImpl<char> BufferA

Definition at line 124 of file menusys.cpp.

typedef std::vector<long> LapArray

Definition at line 127 of file menusys.cpp.

typedef std::map<std::tstring,long> ProcMap

Map the name of a procedure to the offset of that procedure in the script file.

Definition at line 89 of file menusys.cpp.

typedef std::vector<long> ProcStack

Stack of current procedure calls.

Definition at line 93 of file menusys.cpp.

typedef std::map<std::tstring, MSYS_ThreadImpl*> ThreadMap

Definition at line 194 of file menusys.cpp.

typedef std::map<std::tstring,Timer> TimerMap

Definition at line 149 of file menusys.cpp.


Enumeration Type Documentation

enum DISPLAY

Enumerator:
DISPLAY_NONE 
DISPLAY_PROMPT 
DISPLAY_OUTPUT 
DISPLAY_ALL 

Definition at line 151 of file menusys.cpp.

enum PARSE

Enumerator:
PARSE_DEFAULT 
PARSE_ON 
PARSE_OFF 

Definition at line 236 of file menusys.cpp.


Function Documentation

static bool Dispatch ( MSYS_ThreadImpl *  aThread,
TCHAR *  aCommand,
TCHAR *  aArgs,
PARSE  aParse 
) [static]

Definition at line 2281 of file menusys.cpp.

References MSYS_DispatchEntry::cmd, DoExit(), DoShell(), MSYS_DispatchEntry::fn, g_rgDispatch, MSYS_Display(), PARSE_ON, ParseArgs(), and MSYS_DispatchEntry::unquote.

Referenced by InputLoop().

static void DoChdir ( MSYS_Thread thrd,
MSYS_Args aArgs 
) [static]

Definition at line 1317 of file menusys.cpp.

References MSYS_Display(), and MSYS_NeedHelp().

static void DoEcho ( MSYS_Thread thrd,
MSYS_Args aArgs 
) [static]

Definition at line 1293 of file menusys.cpp.

References MSYS_Display(), and MSYS_NeedHelp().

static void DoExit ( MSYS_Thread thrd,
MSYS_Args aArgs 
) [static]

Definition at line 1025 of file menusys.cpp.

References DoThreads(), g_pRootThread, MSYS_Display(), and MSYS_NeedHelp().

Referenced by Dispatch().

static void DoHelp ( MSYS_Thread thrd,
MSYS_Args aArgs 
) [static]

Definition at line 1224 of file menusys.cpp.

References MSYS_DispatchEntry::cmd, DoShell(), MSYS_DispatchEntry::fn, g_rgDispatch, HelpAll(), HelpExternal(), HelpInternal(), MSYS_Display(), MSYS_NeedHelp(), and MSYS_ShowHelp().

static void DoLoop ( MSYS_Thread thrd,
MSYS_Args aArgs 
) [static]

Definition at line 2042 of file menusys.cpp.

References MSYS_Display(), MSYS_NeedHelp(), and ParseTime().

static void DoProc ( MSYS_Thread thrd,
MSYS_Args aArgs 
) [static]

Definition at line 1930 of file menusys.cpp.

References Input(), MSYS_Display(), MSYS_NeedHelp(), and Parse().

static void DoPwd ( MSYS_Thread thrd,
MSYS_Args aArgs 
) [static]

Definition at line 1305 of file menusys.cpp.

References MAX_PATH, MSYS_Display(), and MSYS_NeedHelp().

static void DoRead ( MSYS_Thread thrd,
MSYS_Args aArgs 
) [static]

Definition at line 1379 of file menusys.cpp.

References MSYS_Display(), and MSYS_NeedHelp().

Referenced by DoSetOpt(), MSYS_Process(), and ThreadCreate().

static void DoSetOpt ( MSYS_Thread thrd,
MSYS_Args aArgs 
) [static]

Definition at line 1717 of file menusys.cpp.

References MSYS_DispatchEntry::cmd, DISPLAY_NONE, DoRead(), MSYS_DispatchEntry::fn, g_rgDispatch, GetConsoleType(), MAX_PATH, MSYS_Commands, MSYS_DefaultOptionsFile, MSYS_Display(), MSYS_DisplayOption(), MSYS_NeedHelp(), MSYS_ShowHelp(), MSYS_DispatchEntry::oneHelp, OPTION_PREFIX, OPTION_PREFIX_LEN, and OutputToTeeFile().

static void DoShell ( MSYS_Thread thrd,
MSYS_Args aArgs 
) [static]

Definition at line 1340 of file menusys.cpp.

References MSYS_ShowHelp().

Referenced by Dispatch(), and DoHelp().

static void DoSleep ( MSYS_Thread thrd,
MSYS_Args aArgs 
) [static]

Definition at line 1269 of file menusys.cpp.

References MSYS_Display(), and MSYS_NeedHelp().

static void DoTee ( MSYS_Thread thrd,
MSYS_Args aArgs 
) [static]

Definition at line 1652 of file menusys.cpp.

References MSYS_NeedHelp().

static void DoThreads ( MSYS_Thread thrd,
MSYS_Args aArgs 
) [static]

Definition at line 1577 of file menusys.cpp.

References g_lockThreadsMap, MSYS_Display(), MSYS_NeedHelp(), ThreadCreate(), ThreadJoin(), and ThreadStop().

Referenced by DoExit(), and MSYS_Process().

static void DoTimer ( MSYS_Thread thrd,
MSYS_Args aArgs 
) [static]

Definition at line 2206 of file menusys.cpp.

References MSYS_Display(), MSYS_NeedHelp(), and TimerTell().

static bool FileExists ( const TCHAR *  aFilePath  )  [static]

Definition at line 2356 of file menusys.cpp.

Referenced by MSYS_Process().

static const TCHAR* GetConsoleType ( DISPLAY  aType  )  [static]

Definition at line 1704 of file menusys.cpp.

References DISPLAY_NONE, DISPLAY_OUTPUT, and DISPLAY_PROMPT.

Referenced by DoSetOpt(), and OptConsole().

static void HelpAll ( MSYS_Thread ,
MSYS_Args  
) [static]

Definition at line 1222 of file menusys.cpp.

Referenced by DoHelp(), and MSYS_ShowHelp().

static void HelpExternal ( MSYS_Thread ,
MSYS_Args  
) [static]

Definition at line 1220 of file menusys.cpp.

Referenced by DoHelp(), and MSYS_ShowHelp().

static void HelpInternal ( MSYS_Thread ,
MSYS_Args  
) [static]

Definition at line 1221 of file menusys.cpp.

Referenced by DoHelp(), and MSYS_ShowHelp().

static void InitInternal (  )  [static]

Definition at line 2321 of file menusys.cpp.

References MSYS_DispatchEntry::cmd, g_rgInternal, and MSYS_Commands.

Referenced by MSYS_Process().

static bool Input ( MSYS_ThreadImpl *  aThread,
Buffer aBuf 
) [static]

Definition at line 818 of file menusys.cpp.

Referenced by DoProc(), and InputLoop().

static void* InputLoop ( void *  thrd  )  [static]

Main thread function running the command input and dispatch loop.

Because this is a real thread function it has a different signature depending on the platform this is compiled for.

Definition at line 616 of file menusys.cpp.

References Dispatch(), DISPLAY_PROMPT, Input(), MSYS_Display(), MSYS_GetPrompt(), OutputToConsole(), OutputToTeeFile(), Parse(), PARSE_DEFAULT, PARSE_ON, and StringReplacements().

Referenced by MSYS_Process(), and ThreadCreate().

void MSYS_Display ( MSYS_Thread aThread,
const TCHAR *  aFormat,
  ... 
)

Display a string.

Thread-safe display of text to the console and writing to the tee file when it is enabled. Additionally, this function will replace thread keywords with the actual thread details when thread handling is enabled.

The thread keywords that are replaced are:
{{THREADID}} Numeric thread ID.
{{THREADNAME}} Thread name as assigned at thread creation.

Parameters:
aThread Thread as supplied to the command handler.
aFormat Format string the same as used by printf(). Note that the string character format uses the Windows style of:
TCHAR argument format spec
char char s
wchar_t wchar_t s
char wchar_t S
wchar_t char S

Definition at line 1138 of file menusys.cpp.

References DISPLAY_OUTPUT, OutputToConsole(), OutputToTeeFile(), and StringReplacements().

Referenced by Dispatch(), DoChdir(), DoEcho(), DoExit(), DoHelp(), DoLoop(), DoProc(), DoPwd(), DoRead(), DoSetOpt(), DoSleep(), DoThreads(), DoTimer(), InputLoop(), MSYS_DisplayOption(), MSYS_DisplayWrapped(), MSYS_ShowHelp(), OptConsole(), OptThreads(), ThreadCreate(), ThreadJoin(), ThreadStop(), and TimerTell().

void MSYS_DisplayOption ( MSYS_Thread aThread,
const TCHAR *  aOption,
const TCHAR *  aValue 
)

Display an option to the user in the standard way.

Parameters:
aThread Thread requesting the prompt. If threads have not been enabled, this will always be the main thread.
aOption Option name
aValue Option value. This value will be automatically quoted as necessary such that the value will be a valid setopt statement.

Definition at line 1687 of file menusys.cpp.

References MSYS_Display().

Referenced by DoSetOpt(), OptConsole(), OptThreads(), and OptUtf8().

void MSYS_DisplayWrapped ( MSYS_Thread aThread,
const TCHAR *  aText 
)

Display a string word-wrapped for 80 column display.

This function will word-wrap text and output it using MSYS_Display.

Parameters:
aThread Thread as supplied to the command handler.
aText Text to be displayed.

Definition at line 1160 of file menusys.cpp.

References MSYS_Display().

Referenced by MSYS_ShowHelp().

bool MSYS_NeedHelp ( MSYS_Thread aThread,
MSYS_Handler  aHandler,
MSYS_Args aArgs,
bool  aShowHelp = true 
)

Returns if detailed help has been requested in the arguments.

This function examines the arguments for any instances of "-?" or "/?" to determine if detailed help has been requested for this command. It will ignore those arguments if they have been suppressed with a command prefix.

Parameters:
aThread Thread as supplied to the command handler.
aHandler Command handler function pointer.
aArgs Arguments as supplied to the command handler.
aShowHelp If true and help has been requested, ShowHelp will be automatically called for this handler before it returns.
Return values:
true User has requested detailed help for this command.
false User has not requested detailed help for this command.

Definition at line 1202 of file menusys.cpp.

References MSYS_ShowHelp().

Referenced by DoChdir(), DoEcho(), DoExit(), DoHelp(), DoLoop(), DoProc(), DoPwd(), DoRead(), DoSetOpt(), DoSleep(), DoTee(), DoThreads(), DoTimer(), OptConsole(), OptThreads(), and OptUtf8().

bool MSYS_Process ( const TCHAR *  aInputFile,
bool  aEnableThreads 
)

Start the menu system.

Calling this function starts processing of the console or file input and exits only when the input file has been completed, or the exit command has been encountered.

Note:
The locale should be set correctly before displaying the menu. It is recommended that setlocale(LC_ALL, ""); be executed before calling this function.
Parameters:
aInputFile Input file that should be processed. MSYS will return immediately following processing the end of this file (no console input). If NULL, the input will be taken from the console.
aEnableThreads Should multiple threads be permitted?
Return values:
true Successful completion
false aInputFile could not be executed

Definition at line 737 of file menusys.cpp.

References DoRead(), DoThreads(), FileExists(), g_pRootThread, g_rgDispatch, InitInternal(), InputLoop(), MSYS_DefaultOptionsFile, MSYS_ThreadUserCreate(), and MSYS_Thread::ROOT_THREAD.

void MSYS_ShowHelp ( MSYS_Thread aThread,
MSYS_Handler  aHandler 
)

Display summary help or detailed command help.

Parameters:
aThread Thread as supplied to the command handler.
aHandler Command handler function pointer to display the help for. If this is NULL then summary help will be displayed. Otherwise the detailed help for that command will be displayed.

Definition at line 2610 of file menusys.cpp.

References MSYS_DispatchEntry::cmd, g_rgDispatch, g_rgInternal, HelpAll(), HelpExternal(), HelpInternal(), MSYS_DispatchEntry::longHelp, MSYS_Commands, MSYS_Display(), MSYS_DisplayWrapped(), MSYS_DispatchEntry::oneHelp, OPTION_PREFIX, and OPTION_PREFIX_LEN.

Referenced by DoHelp(), DoSetOpt(), DoShell(), and MSYS_NeedHelp().

static void OptConsole ( MSYS_Thread thrd,
MSYS_Args aArgs 
) [static]

Definition at line 1847 of file menusys.cpp.

References DISPLAY_NONE, DISPLAY_OUTPUT, DISPLAY_PROMPT, GetConsoleType(), MSYS_Display(), MSYS_DisplayOption(), and MSYS_NeedHelp().

static void OptThreads ( MSYS_Thread thrd,
MSYS_Args aArgs 
) [static]

Definition at line 1882 of file menusys.cpp.

References MSYS_Display(), MSYS_DisplayOption(), and MSYS_NeedHelp().

static void OptUtf8 ( MSYS_Thread thrd,
MSYS_Args aArgs 
) [static]

Definition at line 1905 of file menusys.cpp.

References MSYS_DisplayOption(), and MSYS_NeedHelp().

void OutputToConsole ( const TCHAR *  aString  )  [static]

Definition at line 2398 of file menusys.cpp.

References g_lockOutputConsole.

Referenced by InputLoop(), and MSYS_Display().

static void OutputToTeeFile ( MSYS_ThreadImpl *  aThread,
const Buffer aBuf 
) [static]

Definition at line 1106 of file menusys.cpp.

References g_lockOutputTeeFile.

Referenced by DoSetOpt(), InputLoop(), and MSYS_Display().

static void Parse ( TCHAR *  aBuf,
TCHAR *&  aCommand,
TCHAR *&  aArgs 
) [static]

Definition at line 889 of file menusys.cpp.

Referenced by DoProc(), and InputLoop().

static void ParseArgs ( TCHAR *  aBuf,
bool  aUnquote,
MSYS_Args aArgs 
) [static]

Definition at line 922 of file menusys.cpp.

Referenced by Dispatch().

static int ParseTime ( const TCHAR *  aTimeString,
struct tm &  aTime 
) [static]

Definition at line 2016 of file menusys.cpp.

Referenced by DoLoop().

static bool ReplaceString ( Buffer aBuf,
const TCHAR *  aSearch,
const TCHAR *  aReplace 
) [static]

Definition at line 1048 of file menusys.cpp.

Referenced by StringReplacements().

static bool StringReplacements ( MSYS_ThreadImpl *  aThread,
Buffer aBuf 
) [static]

Definition at line 1074 of file menusys.cpp.

References ReplaceString().

Referenced by InputLoop(), and MSYS_Display().

static void ThreadCreate ( MSYS_ThreadImpl *  aThread,
TCHAR *  aThreadName,
TCHAR *  aPath 
) [static]

Definition at line 1428 of file menusys.cpp.

References DISPLAY_NONE, DoRead(), InputLoop(), MSYS_Display(), and MSYS_ThreadUserCreate().

Referenced by DoThreads().

static void ThreadJoin ( MSYS_ThreadImpl *  aThread,
TCHAR *  aThreadName 
) [static]

Definition at line 1505 of file menusys.cpp.

References MSYS_Display().

Referenced by DoThreads().

static void ThreadStop ( MSYS_ThreadImpl *  aThread,
TCHAR *  aThreadName 
) [static]

Definition at line 1550 of file menusys.cpp.

References MSYS_Display().

Referenced by DoThreads().

static void TimerTell ( MSYS_ThreadImpl *  aThread,
const TCHAR *  aTimer,
bool  aShowRaw 
) [static]

Definition at line 2133 of file menusys.cpp.

References MSYS_Display().

Referenced by DoTimer().


Variable Documentation

bool g_bEnableThreads = false [static]

Definition at line 596 of file menusys.cpp.

ThreadLock g_lockOutputConsole [static]

Definition at line 599 of file menusys.cpp.

Referenced by OutputToConsole().

ThreadLock g_lockOutputTeeFile [static]

Definition at line 600 of file menusys.cpp.

Referenced by OutputToTeeFile().

ThreadLock g_lockThreadsMap [static]

Definition at line 598 of file menusys.cpp.

Referenced by DoThreads().

ThreadMap g_mapThreads [static]

Definition at line 597 of file menusys.cpp.

MSYS_ThreadImpl* g_pRootThread = NULL [static]

Definition at line 601 of file menusys.cpp.

Referenced by DoExit(), and MSYS_Process().

MSYS_DispatchEntry* g_rgDispatch = NULL [static]

Definition at line 595 of file menusys.cpp.

Referenced by Dispatch(), DoHelp(), DoSetOpt(), MSYS_Process(), and MSYS_ShowHelp().

const MSYS_DispatchEntry g_rgInternal[]

Definition at line 2448 of file menusys.cpp.

Referenced by InitInternal(), and MSYS_ShowHelp().

const MSYS_DispatchEntry g_rgInternal[]

Definition at line 2448 of file menusys.cpp.

Referenced by InitInternal(), and MSYS_ShowHelp().

const TCHAR* MSYS_HEADER = _T("")

const TCHAR OPTION_PREFIX[] = _T("_setopt_") [static]

Definition at line 75 of file menusys.cpp.

Referenced by DoSetOpt(), and MSYS_ShowHelp().

const size_t OPTION_PREFIX_LEN = (sizeof(OPTION_PREFIX) / sizeof(TCHAR)) - 1 [static]

Definition at line 76 of file menusys.cpp.

Referenced by DoSetOpt(), and MSYS_ShowHelp().


Generated on Wed Jun 20 09:28:35 2007 for menusys by  doxygen 1.5.2