File: options.c

package info (click to toggle)
teg 0.9.2-2.1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 5,232 kB
  • ctags: 2,372
  • sloc: ansic: 18,999; sh: 9,273; makefile: 589; yacc: 318; xml: 160
file content (255 lines) | stat: -rw-r--r-- 6,126 bytes parent folder | download
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*	$Id: options.c,v 1.6 2001/09/03 00:55:40 riq Exp $	*/
/* Tenes Empanadas Graciela
 *
 * Copyright (C) 2000 Ricardo Quesada
 *
 * Author: Ricardo Calixto Quesada <rquesada@core-sdi.com>
 *
 * 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; only version 2 of the License
 *
 * 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */
/**
 * @file options.c
 * Manages the server options
 */

#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "server.h"

TEG_STATUS option_conqworld(int fd, char *str);
TEG_STATUS option_conqworld_view( void );
TEG_STATUS option_cmission( int fd, char *str );
TEG_STATUS option_cmission_view( void );
TEG_STATUS option_armies(int fd, char *str);
TEG_STATUS option_armies_view( void );
TEG_STATUS option_seed(int fd, char *str);
TEG_STATUS option_seed_view( void );
TEG_STATUS option_help( int fd, char *str );

TEG_STATUS option_lookup( int fd, PARSER *p );

struct {
	char *label;
	TEG_STATUS (*func) ();
	TEG_STATUS (*func_view) ();
	char *help;
} options[] = {
	{ OPTION_CONQWORLD, option_conqworld, option_conqworld_view, N_("Conquer the world, or play with secret missions") },
	{ OPTION_CMISSION, option_cmission, option_cmission_view, N_("Enable/Disable common secret mission") },
	{ OPTION_ARMIES, option_armies, option_armies_view, N_("Set the initial number of armies to place") },
	{ OPTION_SEED, option_seed, option_seed_view, N_("Set the seed for random") },
	{ OPTION_HELP, option_help, NULL, N_("Shows the set options") },
};
#define	NOPTIONS  (sizeof(options)/sizeof(options[0]))

TEG_STATUS option_conqworld_view( void )
{
	con_text_out_wop(M_INF,_("%s variable is set to: %s\n"),
		OPTION_CONQWORLD,
		(!g_juego.objetivos?_("TRUE"):_("FALSE"))
		);
	return TEG_STATUS_SUCCESS;
}
TEG_STATUS option_conqworld( int fd, char *str )
{
	int a,b;

	if( str && strlen(str) !=0 ) {
		a = atoi(str);
		b = !g_juego.objetivos;
		if( objetivo_set( !a ) == TEG_STATUS_SUCCESS ) {
			con_text_out_wop(M_INF,_("Setting %s variable to: %s -> %s\n"),
				OPTION_CONQWORLD,
				( b?_("TRUE"):_("FALSE")),
				( a?_("TRUE"):_("FALSE"))
				);
		} else goto error;
	} else {
		option_conqworld_view();
	}
	return TEG_STATUS_SUCCESS;

error:
	if(fd==CONSOLE_FD)
		con_text_out_wop(M_ERR,_("Error setting %s variable.\n"),OPTION_CONQWORLD);

	return TEG_STATUS_ERROR;
}

TEG_STATUS option_cmission_view( void )
{
	con_text_out_wop(M_INF,_("%s variable is set to: %s\n"),
		OPTION_CMISSION,
		(g_juego.cmission?_("TRUE"):_("FALSE"))
		);
	return TEG_STATUS_SUCCESS;
}

TEG_STATUS option_cmission( int fd, char *str )
{
	int a,b;

	if( str && strlen(str) !=0 ) {
		a = atoi(str);
		b = g_juego.cmission;
		if( objetivo_common_mission( a ) == TEG_STATUS_SUCCESS ) {
			con_text_out_wop(M_INF,_("Setting %s variable to: %s -> %s\n"),
				OPTION_CMISSION,
				( b?_("TRUE"):_("FALSE")),
				( a?_("TRUE"):_("FALSE"))
				);
		} else goto error;
	} else {
		option_cmission_view();
	}
	return TEG_STATUS_SUCCESS;

error:
	if(fd==CONSOLE_FD)
		con_text_out_wop(M_ERR,_("Error setting %s variable.\n"),OPTION_CMISSION);

	return TEG_STATUS_ERROR;
}



TEG_STATUS option_armies_view( void )
{
	con_text_out_wop(M_INF,_("%s variable is set to: (%d,%d)\n"),OPTION_ARMIES,g_juego.fichas,g_juego.fichas2);
	return TEG_STATUS_SUCCESS;
}

TEG_STATUS option_armies( int fd, char *str )
{
	int armies1;
	int armies2;
	PARSER p;
	DELIM igualador=DELIM_EQ3;
	DELIM separador={ ',', ',', ',' };

	p.igualador = &igualador;
	p.separador = &separador;
	p.data = str;

	if(str && strlen(str)!=0) {

		if( JUEGO_EMPEZADO )
			goto error;

		if( parser_call( &p ) && p.hay_otro ) {
			armies1 = atoi( p.token );		
		} else goto error;

		if( parser_call( &p ) && !p.hay_otro ) {
			armies2 = atoi( p.token );		
		} else goto error;

		if(armies1 && armies2 ) {
			g_juego.fichas = armies1;
			g_juego.fichas2 = armies2;
		} else goto error;
	}
	option_armies_view();
	return TEG_STATUS_SUCCESS;
error:
	return TEG_STATUS_ERROR;
}

TEG_STATUS option_seed_view( void )
{
	con_text_out_wop(M_INF,_("%s variable is set to: %u\n"),OPTION_SEED,g_juego.seed);
	return TEG_STATUS_SUCCESS;
}

TEG_STATUS option_seed( int fd, char *str )
{
	unsigned int seed;

	if(str && strlen(str)!=0) {

		if(fd!=CONSOLE_FD)
			goto error;

		seed = atoi(str);
		g_juego.seed = seed;
		srandom(seed);
	}

	option_seed_view();
	return TEG_STATUS_SUCCESS;
error:
	return TEG_STATUS_ERROR;
}

TEG_STATUS option_help( int fd, char *str )
{
	int i;
	for(i=0;i<NOPTIONS;i++) {
		if(options[i].func)
			net_printf(fd, TOKEN_REM"='%s' %s\n",options[i].label,_(options[i].help));
	}
	return TEG_STATUS_SUCCESS;
}

TEG_STATUS option_lookup( int fd, PARSER *p )
{
	int i;

	for(i = 0; i < NOPTIONS; i++) {
		if(strcasecmp( p->token, options[i].label )==0 ){
			if (options[i].func)
				return( (options[i].func)(fd,p->value));

			return TEG_STATUS_TOKENNULL;
		}
	}
	net_printf(fd,_("Unknow option. Try using `%s %s' for help\n"),TOKEN_SET,OPTION_HELP);
	return TEG_STATUS_TOKENNOTFOUND;
}

TEG_STATUS option_view( int fd, char *str)
{
	int i;

	for(i = 0; i < NOPTIONS; i++) {
		if (options[i].func_view)
			(options[i].func_view)();
	}
	return TEG_STATUS_SUCCESS;
}

TEG_STATUS option_parse( int fd, char *str)
{
	int i;
	PARSER p;
	DELIM igualador={ '=', ' ', ':' };
	DELIM separador=DELIM_NULL;

	p.igualador = &igualador;
	p.separador = &separador;
	p.data = str;

	do {
		if( (i=parser_call( &p )) ) {
			if( option_lookup( fd,&p ) == TEG_STATUS_CONNCLOSED ) {
				return TEG_STATUS_CONNCLOSED;
			}
		}
	} while( i && p.hay_otro);

	return TEG_STATUS_SUCCESS;
}