summaryrefslogtreecommitdiff
path: root/src/plugins/console.c
blob: fca0876182686a5cc15a68e0d2af04e1c527342a (plain) (blame)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Copyright (c) Athena Dev Teams - Licensed under GNU GPL
// For more information, see LICENCE in the main folder

#include "../common/plugin.h"

#ifdef WIN32
	#define WIN32_LEAN_AND_MEAN
	#include <windows.h>
#else
	#include <sys/types.h>
	#include <unistd.h>
	#include <poll.h>
	#include <string.h>
#endif
#include <stdio.h> // stdin, fgets

#ifdef WIN32

#define THREAD_FUNC_START(name) DWORD WINAPI thread_ ## name(LPVOID lpParameter) { (void)lpParameter; {
#define THREAD_FUNC_END(name) } ExitThread(0); return 0; }
#define THREAD_EXECUTE(name,errvar) \
	do{ \
		int _fail_ = (CreateThread(NULL,0,thread_ ## name,NULL,0,NULL) == NULL); \
		if( errvar ) \
			*errvar = _fail_; \
	}while(0)
#define sleep Sleep

#define pipe_create(p) (CreatePipe(&p[PIPE_READ], &p[PIPE_WRITE], NULL, 1) == 0)
#define pipe_read(p,data,len) do{ DWORD _b_; ReadFile(p[PIPE_READ], data, len, &_b_, NULL); }while(0)
#define pipe_write(p,data,len) do{ DWORD _b_; WriteFile(p[PIPE_WRITE], data, len, &_b_, NULL); }while(0)
#define pipe_close(p,side) CloseHandle(p[side])
typedef HANDLE PIPE[2];

#else

#define THREAD_FUNC_START(name) void thread_ ## name(void) {
#define THREAD_FUNC_END(name) _exit(0); }
#define THREAD_EXECUTE(name,errvar) \
	do{ \
		int pid = fork(); \
		if( pid == 0 ){ \
			thread_ ## name(); \
		} \
		if( errvar ) \
			*errvar = (pid == -1); \
	}while(0)

#define pipe_create(p) pipe(p)
#define pipe_read(p,data,len) read(p[PIPE_READ], data, len)
#define pipe_write(p,data,len) write(p[PIPE_WRITE], data, len)
#define pipe_close(p,side) close(p[side])
typedef int PIPE[2];

#endif

#define PIPE_READ 0
#define PIPE_WRITE 1
#define INPUT_BUFSIZE 4096

////// Plugin information ////////
//
PLUGIN_INFO = {
	"Console", // Name
	PLUGIN_ALL, // Target servers
	"0.1", // Version
	"1.03", // Minimum plugin engine version to run
	"Console parser" // Short description
};

////// Plugin event list //////////
// Format: <plugin function>,<event name>
// All registered functions to a event gets executed
// (In descending order) when its called.
// Multiple functions can be called by multiple events too,
// So it's up to your creativity ^^
//
PLUGIN_EVENTS_TABLE = {
	{ "console_init",  EVENT_PLUGIN_INIT },
	{ "console_final", EVENT_PLUGIN_FINAL },
	{ "console_start", EVENT_ATHENA_INIT },
	{ "console_stop",  EVENT_ATHENA_FINAL },
	{ NULL, NULL }
};

///// Variables /////

typedef int TimerFunc(int tid, unsigned int tick, int id, int data);
int (*add_timer_func_list)(TimerFunc func, char* name);
int (*add_timer_interval)(unsigned int tick, TimerFunc* func, int id, int data, int interval);
int (*delete_timer)(int tid, TimerFunc* func);
unsigned int (*gettick)(void);
int (*parse_console)(char* buf);

int tid;
PIPE data;
PIPE next;

//////// Plugin functions //////////

static int pipe_hasdata(PIPE p)
{
#ifdef WIN32
	return ( WaitForSingleObject(p[PIPE_READ],0) == WAIT_OBJECT_0 );
#else
	struct pollfd fds;
	fds.fd = p[PIPE_READ];
	fds.events = POLLRDNORM;
	return ( poll(&fds,1,0) > 0 );
#endif
}

int console_parsebuf(int tid, unsigned int tick, int id, int data_)
{
	//printf("console_parsebuf\n");
	//delete_timer(tid, console_parsebuf);
	if( pipe_hasdata(data) ){
		char buf[INPUT_BUFSIZE];
		size_t len;
		//printf("console_parsebuf pipe_hasdata\n");
		// receive string
		pipe_read(data, &len, sizeof(size_t));
		pipe_read(data, &buf, len);
		buf[len] = '\0';
		//printf("console_parsebuf buf='%s'\n", buf);
		// parse it
		parse_console(buf);
		//printf("console_parsebuf writing next\n");
		// send next state
		buf[0] = 'R';
		pipe_write(next, &buf, 1);
		//printf("console_parsebuf done with next\n");
	}
	return 0;
}

THREAD_FUNC_START(readinput)
	char buf[INPUT_BUFSIZE];
	char state = 'R';
	size_t len;

	//printf("thread_readinput START\n");
	pipe_close(data, PIPE_READ);
	pipe_close(next, PIPE_WRITE);
	buf[sizeof(buf)-1] = '\0';
	for( ; state != 'X'; )
	{
		//printf("thread_readinput getting data\n");
		buf[0] = '\0';
		fgets(buf, sizeof(buf)-1, stdin);
		len = strlen(buf);
		//printf("thread_readinput buf='%s'\n", buf);
		// send string
		pipe_write(data, &len, sizeof(size_t));
		pipe_write(data, buf, len);
		// receive next state
		pipe_read(next, &state, sizeof(char));
	}
	pipe_close(data, PIPE_WRITE);
	pipe_close(next, PIPE_READ);
	//printf("thread_readinput STOP (%d)\n", state);
THREAD_FUNC_END(readinput)

void console_start(void)
{
	int error = 0;
	//printf("console_start\n");
	if( pipe_create(data) ){
		//printf("console_start data pipe failed\n");
		return;
	}
	if( pipe_create(next) ){
		//printf("console_start next pipe failed\n");
		pipe_close(data, PIPE_READ);
		pipe_close(data, PIPE_WRITE);
		return;
	}
	THREAD_EXECUTE(readinput, &error);
	if( error ){
		//printf("console_start thread start error\n");
		pipe_close(data, PIPE_READ);
		pipe_close(next, PIPE_WRITE);
	} else {
		//printf("console_start thread started\n");
		//parse_console("help");
		add_timer_func_list(console_parsebuf,"console_parsebuf");
		tid = add_timer_interval(gettick(),console_parsebuf,0,0,1); // run once every cycle
	}
	pipe_close(data, PIPE_WRITE);
	pipe_close(next, PIPE_READ);
}

void console_stop(void)
{
	char c = 'X';
	//printf("console_stop\n");
	if( tid != -1 ){
		delete_timer(tid, console_parsebuf);
		pipe_write(next, &c, sizeof(char));
	}
	return;
}

void console_init(void)
{
	//printf("console_init\n");
	// import symbols
	IMPORT_SYMBOL(add_timer_interval, SYMBOL_ADD_TIMER_INTERVAL);
	IMPORT_SYMBOL(add_timer_func_list, SYMBOL_ADD_TIMER_FUNC_LIST);
	IMPORT_SYMBOL(delete_timer, SYMBOL_DELETE_TIMER);
	IMPORT_SYMBOL(gettick, SYMBOL_GETTICK);
	IMPORT_SYMBOL(parse_console, SYMBOL_PARSE_CONSOLE);
	//printf("%d -> add_timer_func_list=0x%x\n", SYMBOL_ADD_TIMER_FUNC_LIST, (int)add_timer_func_list);
	//printf("%d -> add_timer_interval=0x%x\n", SYMBOL_ADD_TIMER_INTERVAL, (int)add_timer_interval);
	//printf("%d -> delete_timer=0x%x\n", SYMBOL_DELETE_TIMER, (int)delete_timer);
	//printf("%d -> gettick=0x%x\n", SYMBOL_GETTICK, (int)gettick);
	//printf("%d -> parse_console=0x%x\n", SYMBOL_PARSE_CONSOLE, (int)parse_console);

	return;
}

void console_final(void)
{
	//printf("console_final\n");
	return;
}