forked from psi46/psi46test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.h
130 lines (97 loc) · 3.47 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
// command.h: Schnittstelle fuer die Klasse 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 <list>
#include "htable.h"
#include "config.h"
class CInterpreter;
#define CMDLINELENGTH 200
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 void(*CMDFUNCTION)(CCmdLine &);
class CCommand
{
const char *m_name;
const char *m_parameter;
const char *m_help;
CMDFUNCTION m_exec;
public:
friend class CInterpreter;
};
class CHelpCategory
{
const char *m_name;
std::list<CCommand> helpList;
public:
friend class CInterpreter;
};
class CInterpreter
{
CHelpCategory *currentHelpCat;
std::list<CHelpCategory> helpCategory;
CHashTable<CCommand> cmdList;
CCmdLine cmdline;
char scriptPath[256];
void ListHelpCategories();
void ListHelpText(std::list<CHelpCategory>::iterator cat);
void help();
public:
CInterpreter();
~CInterpreter() {};
void SetScriptPath(const char path[]);
void AddHelpCategory(const char name[]);
void AddCommand(const char name[], CMDFUNCTION f, const char parameter[], const char help[]);
bool run(FILE *f, int iter = 0);
};
bool cmd_not_implemented(CCmdLine &par);
extern CInterpreter cmd_intp;
#define HELP_CAT(name)
#define CMD_REG(name,parameter,helptext) void cmd_##name(CCmdLine &par);
#define CMD_PROC(name) void cmd_##name(CCmdLine &par)
#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("illegal integer parameter!\n"); return; }
#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("illegal range parameter!\n"); return; }
#define PAR_STRING(var,size) if (!par.getString(var,size)) \
{ printf("illegal string parameter!\n"); return; }
#define PAR_IS_STRING(var,size) (par.getString(var,size))
#define PAR_STRINGEOL(var,size) if (!par.getStringEOL(var,size)) \
{ printf("illegal string parameter!\n"); return; }
#endif // !defined(AFX_COMMAND_H__CE717DC8_3D70_42BB_AA0A_B9F4C73250A5__INCLUDED_)