File: misc.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 (331 lines) | stat: -rw-r--r-- 6,731 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*	$Id: misc.c,v 1.49 2001/12/16 01:29:46 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 misc.c
 */

#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>

#include <glib.h>

#include "client.h"

#ifdef WITH_GGZ
#include "ggz_client.h"
#endif /* WITH_GGZ */

CJUEGO g_juego;			/**< client juego */

static void close_descriptors( void )
{
	int i, open_max;

	/* force manually the close of the socket */
	if( g_juego.fd >0 ) close (g_juego.fd);

	open_max = sysconf (_SC_OPEN_MAX);
	for (i = 3; i < open_max; i++)
		fcntl (i, F_SETFD, FD_CLOEXEC);
}

/**
 * @fn TEG_STATUS juego_init()
 * Inicializa un juego
 */
TEG_STATUS juego_reinit()
{
	int i;

	g_juego.jugadores = 0;
	g_juego.dados_srcpais=-1;
	g_juego.dados_dstpais=-1;
	for(i=0;i<3;i++) {
		g_juego.dados_src[i] = 0;
		g_juego.dados_dst[i] = 0;
	}

	InitializeListHead( &g_juego.tarjetas_list );
	g_juego.tarjetas_cant = 0;

	g_juego.objetivo = -1;
	g_juego.reglas = TEG_RULES_TEG;

	return TEG_STATUS_SUCCESS;
}


/**
 * @fn TEG_STATUS juego_init()
 * Inicializa un juego
 */
TEG_STATUS juego_init()
{
	static int firsttime=1;
	g_juego.fd = -1;
	g_juego.numjug = -1;
	g_juego.observer = 0;

	/* variables seteadas por command line y que pueden cambiar estos defaults */
	if(firsttime) {
		dirs_create();
		g_juego.with_ggz = 0;
	}

	ESTADO_SET(JUG_ESTADO_DESCONECTADO);

	juego_reinit();

	/* name, server, serverport, color lo asiga la gui-xxx */

	firsttime=0;
	return TEG_STATUS_SUCCESS;
}

/**
 * @fn TEG_STATUS juego_finalize()
 * Termina un juego preparandolo para otro
 */
TEG_STATUS juego_finalize()
{
	paises_init();
	juego_reinit();

	return TEG_STATUS_SUCCESS;
}

/**
 * @fn TEG_STATUS conectar()
 * Se conectar un server
 */
TEG_STATUS conectar()
{
	if( ESTADO_ES(JUG_ESTADO_DESCONECTADO)) {

		/* standar mode */
		if( !g_juego.with_ggz ) {
			g_juego.fd = net_connect_tcp( (char*) &g_juego.sername, g_juego.serport );
			if( g_juego.fd < 0) {
				textmsg(M_ERR,_("Error while trying to connect to server '%s' at port %d"),g_juego.sername,g_juego.serport);
				return TEG_STATUS_ERROR;
			}

		} else {

#ifdef WITH_GGZ
			/* GGZ mode */
			g_juego.fd = ggz_client_connect();
			if( g_juego.fd < 0 ) {
				textmsg(M_ERR,_("Error while trying to connect to GGZ client"));
				return TEG_STATUS_ERROR;
			}
		}
#endif /* WITH_GGZ */

		if( !g_juego.with_ggz ) {
			out_pversion();
		}

		return TEG_STATUS_SUCCESS;
	} else {
		textmsg(M_ERR,_("Error, you are already connected"));
		return TEG_STATUS_ERROR;
	}
}

/**
 * @fn TEG_STATUS desconectar()
 * Se desconecta del server
 */
TEG_STATUS desconectar()
{
	ESTADO_SET(JUG_ESTADO_DESCONECTADO);
	gui_disconnect();

	jugador_flush();

	if( g_juego.fd > 0 ) {
		close( g_juego.fd );
		g_juego.fd = -1;
	}

	juego_finalize();
	juego_init();


	return TEG_STATUS_SUCCESS;
}

/**
 * @fn TEG_STATUS playerid_restore_from_error( void )
 */
TEG_STATUS playerid_restore_from_error( void )
{
	textmsg( M_ERR, _("The game has already started. Connect as an observer."));
	desconectar();
	return TEG_STATUS_SUCCESS;
}

/**
 * @fn TEG_STATUS launch_server( int port )
 * Launch a server in localost
 */
TEG_STATUS launch_server( int port )
{
	pid_t pid;
	char *args[5];

	if ( (pid = fork()) < 0) {
		perror("tegclient:");
		return TEG_STATUS_ERROR;
	} else if (pid == 0) {

		char buffer[100];
		close_descriptors();
		args[0] = "xterm";
		args[1] = "-e";
		args[2] = BINDIR"/tegserver";
		args[3] = "--port";
		snprintf(buffer, sizeof(buffer)-1, "%d", port); 
		buffer[ sizeof(buffer)-1 ] = 0;
		args[4] = buffer;
		args[5] = NULL;

		if( execvp(args[0], args) < 0) {
			perror(args[0]);
			/* last chance, launch tegserver without console */
			args[0] = BINDIR"/tegserver";
			args[1] = "--console";
			args[2] = "0";
			args[3] = "--port";
			snprintf(buffer, sizeof(buffer)-1, "%d", port); 
			buffer[ sizeof(buffer)-1 ] = 0;
			args[4] = buffer;
			args[5] = NULL;
			if( execv(args[0], args) < 0) {
				fprintf(stderr,"Launching server failed. Does the file '%s' exists ?\n",args[0]);
				perror("exe:");

				/* This saves a crash */
				args[0] = "/bin/true";
				args[1] = NULL;
				execv(args[0],args);
				exit(1);
			}
		}
		return TEG_STATUS_ERROR;
	} else {
		/* wait util server is launched */
		sleep(2);
	}
	return TEG_STATUS_SUCCESS;
}

/**
 * @fn TEG_STATUS launch_robot( void )
 * Launch robot in localhost
 */
TEG_STATUS launch_robot( void )
{
	pid_t pid;
	char *args[6];
	char port[50];

	if ( (pid = fork()) < 0) {
		perror("tegclient:");
		return TEG_STATUS_ERROR;
	} else if (pid == 0) {

		close_descriptors();

		sprintf(port,"%d",g_juego.serport);

		args[0] = BINDIR"/tegrobot";
		args[1] = "--server";
		args[2] = g_juego.sername;
		args[3] = "--port";
		args[4] = port;
		args[5] = "--quiet";
		args[6] = NULL;
	
		if( execv(args[0], args) < 0) {
			fprintf(stderr,"Launching robot failed. Does the file `%s' exists ?\n",args[0]);
			perror("exe:");

			/* This saves a crash */
			args[0] = "/bin/true";
			args[1] = NULL;
			execv(args[0],args);
			exit(1);
		}
		return TEG_STATUS_ERROR;
	} else {
		/* nothing */
	}
	return TEG_STATUS_SUCCESS;
}

TEG_STATUS textmsg( int level, char *format, ...)
{
        va_list args;
	char buf[PROT_MAX_LEN];

	va_start(args, format);
	vsnprintf(buf, sizeof(buf) -1, format, args);
	va_end(args);

	buf[ sizeof(buf) -1 ] = 0;

	if( g_juego.msg_show & level )
		gui_textmsg(buf);
	return TEG_STATUS_SUCCESS;
}

/**
 * @fn TEG_STATUS dirs_create()
 * Creates defaults dir for TEG
 */
TEG_STATUS dirs_create()
{
	DIR *dir;
	char buf[1000];

	memset(buf,0,sizeof(buf));

	snprintf(buf,sizeof(buf)-1,"%s/%s",g_get_home_dir(),TEG_DIRRC);

	if( (dir = opendir(buf)) == NULL )
		mkdir(buf,0755);
	else
		closedir(dir);

	snprintf(buf,sizeof(buf)-1,"%s/%s/themes",g_get_home_dir(),TEG_DIRRC);
	if( (dir = opendir(buf)) == NULL )
		mkdir(buf,0755);
	else
		closedir(dir);

	return TEG_STATUS_SUCCESS;
}