-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.h
138 lines (114 loc) · 3.51 KB
/
command.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// class CCmdLine
// class CCommand
// class CInterpreter
#if !defined(AFX_COMMAND_H__CE717DC8_3D70_42BB_AA0A_B9F4C73250A5__INCLUDED_)
#define AFX_COMMAND_H__CE717DC8_3D70_42BB_AA0A_B9F4C73250A5__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <stdio.h>
#include <string.h>
#include "htable.h"
#include "version.h"
class CInterpreter;
#define CMDLINELENGTH 80
bool keypressed( );
#define MAXSYMLEN 10
struct CSymbol
{
char name[MAXSYMLEN + 1];
int value;
};
//------------------------------------------------------------------------------
class CCmdLine
{
bool interactive;
char s[CMDLINELENGTH + 1];
char *cmd, *par;
bool read( FILE * f );
bool isCmd( const char name[] )
{
return strcmp( cmd, name ) == 0;
}
static const CSymbol symtable[];
static bool isAlpha( char ch )
{
return ( 'A' <= ch && ch <= 'Z' ) || ( 'a' <= ch && ch <= 'z' );
}
static bool isNumber( char ch )
{
return '0' <= ch && ch <= '9';
}
static bool isAlphaNum( char ch )
{
return isAlpha( ch ) || isNumber( ch );
}
static bool isWhitespace( char ch )
{
return ch == ' ' || ch == '\t';
}
static int GetHex( char ch );
bool getSymbol( int &value );
bool getNumber( int &value );
void setInteractive( bool on )
{
interactive = on;
}
public:
const char *getName( )
{
return cmd;
}
bool getInt( int &value, int min, int max );
bool getIntRange( int &valuemin, int &valuemax, int skipmin, int skipmax );
bool getString( char *value, int size );
bool getStringEOL( char *value, int size );
bool isInteractive( )
{
return interactive;
}
friend class CInterpreter;
};
typedef bool( *CMDFUNCTION ) ( CCmdLine & );
//------------------------------------------------------------------------------
class CCommand
{
const char *m_help;
CMDFUNCTION m_exec;
public:
friend class CInterpreter;
};
//------------------------------------------------------------------------------
class CInterpreter
{
CHashTable < CCommand > cmdList;
CCmdLine cmdline;
char scriptPath[256];
void help( );
public:
CInterpreter( );
~CInterpreter( )
{
};
void SetScriptPath( const char path[] );
void AddCommand( const char name[], CMDFUNCTION f, const char help[] );
bool run( FILE * f, int iter = 0 );
};
//------------------------------------------------------------------------------
bool cmd_not_implemented( CCmdLine & par );
extern CInterpreter cmd_intp;
#define CMD_PROC( name ) bool cmd_##name( CCmdLine &par )
#define CMD_REG( name, help ) cmd_intp.AddCommand( #name, cmd_##name, help )
#define CMD_NUL( name, help ) cmd_intp.AddCommand( #name, cmd_not_implemented, help )
#define CMD_RUN( file ) cmd_intp.run( file );
#define PAR_INT( var, min, max ) if( !par.getInt( var, (min), (max) ) ) \
{ printf( "missing or illegal integer parameter!\n"); return false; }
#define PAR_IS_INT( var, min, max ) par.getInt( var, (min), (max) )
#define PAR_RANGE( varmin, varmax, min, max ) if( !par.getIntRange( varmin, varmax, (min), (max) ) ) \
{ printf( "missing or illegal range parameter!\n" ); return false; }
#define PAR_STRING( var, size ) if( !par.getString( var, size ) ) \
{ printf( "missing or illegal string parameter!\n" ); return false; }
#define PAR_IS_STRING( var, size ) ( par.getString( var, size ) )
#define PAR_STRINGEOL( var, size ) if( !par.getStringEOL( var, size ) ) \
{ printf( "missing or illegal string parameter!\n" ); return false; }
#endif // !defined(AFX_COMMAND_H__CE717DC8_3D70_42BB_AA0A_B9F4C73250A5__INCLUDED_)