aboutsummaryrefslogtreecommitdiff
path: root/saga/cvar.cpp
blob: 41d52f6867bf42f21b907f1a788c1c75587b4a54 (plain)
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2004 The ScummVM project
 *
 * The ReInherit Engine is (C)2000-2003 by Daniel Balsom.
 *
 * 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; either version 2
 * of the License, or (at your option) any later version.

 * 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.
 *
 * $Header$
 *
 */
/*
 Description:   
 
    Configuration Variable Module

 Notes: 
*/

#include "reinherit.h"

#include <limits.h>
#include <stddef.h>

/*
   Uses the following modules:
\*--------------------------------------------------------------------------*/
#include "console_mod.h"

/*
   Begin module
\*--------------------------------------------------------------------------*/
#include "cvar_mod.h"
#include "cvar.h"

namespace Saga {

R_CVAR *CVHashTbl[R_CVAR_HASHLEN];

static const char *CVAR_ErrMsg[] = {

	"No Error",
	"Not implememented.",
	"Memory allocation failed",
	"Value overflowed while parsing",
	"Invalid numeric constant",
	"Value overflows destination type",
	"Assignment of negative value to unsigned variable",
	"Value outside of specified bounds",
	"Invalid string literal",
	"Invalid type for assignment",
	"Variable is read-only",
	"Not a valid function"
};

enum CVAR_Errors {

	CVERR_NONE,
	CVERR_NOTIMPL,
	CVERR_MEM,
	CVERR_PARSEOVERFLOW,
	CVERR_INVALID,
	CVERR_DESTOVERFLOW,
	CVERR_SIGN,
	CVERR_BOUND,
	CVERR_STRING,
	CVERR_TYPE,
	CVERR_READONLY,
	CVERR_NOTFUNC
};

static enum CVAR_Errors CVAR_ErrorState;

int CVAR_GetError(const char **err_str)
/****************************************************************************\
 Returns the appropriate cvar error string
\****************************************************************************/
{

	*err_str = CVAR_ErrMsg[CVAR_ErrorState];

	return CVAR_ErrorState;
}

int CVAR_Shutdown(void)
/****************************************************************************\
 Frees the cvar hash table
\****************************************************************************/
{

	R_CVAR *walk_ptr;
	R_CVAR *temp_ptr;
	int i;

	R_printf(R_STDOUT, "CVAR_Shutdown(): Deleting cvar hash table.\n");

	for (i = 0; i < R_CVAR_HASHLEN; i++) {

		for (walk_ptr = CVHashTbl[i]; walk_ptr; walk_ptr = temp_ptr) {

			temp_ptr = walk_ptr->next;
			free(walk_ptr);
		}
	}

	return R_SUCCESS;
}

unsigned int CVAR_HashString(const char *str)
/****************************************************************************\
 Returns hash index for string 'str'.
 Cannot fail.
\****************************************************************************/
{

	unsigned int index;

	for (index = 0; *str != '\0'; str++) {
		index = *str + 31 * index;
	}

	return index % R_CVAR_HASHLEN;
}

int CVAR_Add(int index, R_CVAR * cvar)
/****************************************************************************\
 Adds a copy of the given cvar into the hash table.
 Returns R_SUCCESS if cvar was added, R_MEM if allocation failed.
\****************************************************************************/
{

	R_CVAR *new_cvar;
	R_CVAR *temp_ptr;

	new_cvar = (R_CVAR *)malloc(sizeof(R_CVAR));

	if (new_cvar == NULL) {
		CVAR_ErrorState = CVERR_MEM;
		return R_MEM;
	}

	memcpy(new_cvar, cvar, sizeof(R_CVAR));

	if (CVHashTbl[index] == NULL) {
		CVHashTbl[index] = new_cvar;
		new_cvar->next = NULL;
	} else {
		temp_ptr = CVHashTbl[index];
		CVHashTbl[index] = new_cvar;
		new_cvar->next = temp_ptr;
	}

	CVAR_ErrorState = CVERR_NONE;
	return R_SUCCESS;
}

int CVAR_Exec(R_CVAR_P cvar_func, char *r_value)
/****************************************************************************\
 Attempts to execute the specified console function with the given argument
 string.
 Returns R_FAILURE if cvar_func is not a valid console function
\****************************************************************************/
{

	int cf_argc = 0;
	char **cf_argv = NULL;
	int max_args;

	if (cvar_func->type != R_CVAR_FUNC) {
		CVAR_ErrorState = CVERR_NOTFUNC;
		return R_FAILURE;
	}

	cf_argc = EXPR_GetArgs(r_value, &cf_argv);

	if (cf_argc < cvar_func->t.func.min_args) {
		CON_Print("Too few arguments to function.");
		if (cf_argv)
			free(cf_argv);
		return R_FAILURE;
	}

	max_args = cvar_func->t.func.max_args;
	if ((max_args > -1) && (cf_argc > max_args)) {
		CON_Print("Too many arguments to function.");
		if (cf_argv)
			free(cf_argv);
		return R_FAILURE;
	}

	/* Call function */
	(cvar_func->t.func.func_p) (cf_argc, cf_argv);

	if (cf_argv)
		free(cf_argv);

	return R_SUCCESS;
}

int CVAR_SetValue(R_CVAR_P cvar, char *r_value)
/****************************************************************************\
 Attempts to assign the value contained in the string 'r_value' to cvar.
 Returns R_FAILURE if there was an error parsing 'r_value'
\****************************************************************************/
{

	long int int_param;
	unsigned long uint_param;

	char *end_p;
	ptrdiff_t scan_len;
	int r_value_len;

	r_value_len = strlen(r_value);

	if (cvar->flags & R_CVAR_READONLY) {
		CVAR_ErrorState = CVERR_READONLY;
		return R_FAILURE;
	}

	switch (cvar->type) {

	case R_CVAR_INT:

		int_param = strtol(r_value, &end_p, 10);

		if ((int_param == LONG_MIN) || (int_param == LONG_MAX)) {
			CVAR_ErrorState = CVERR_PARSEOVERFLOW;
			return R_FAILURE;
		}

		scan_len = end_p - r_value;

		if (int_param == 0) {
			if (!scan_len || r_value[scan_len - 1] != '0') {
				/* strtol() returned 0, but string isn't "0". Invalid. */
				CVAR_ErrorState = CVERR_INVALID;
				return R_FAILURE;
			}
		}

		if (scan_len != r_value_len) {
			/* Entire string wasn't converted...Invalid */
			CVAR_ErrorState = CVERR_INVALID;
			return R_FAILURE;
		}

		if ((int_param < CV_INTMIN) || (int_param > CV_INTMAX)) {
			/* Overflows destination type */
			CVAR_ErrorState = CVERR_DESTOVERFLOW;
			return R_FAILURE;
		}

		/* Ignore bounds if equal */
		if (cvar->t.i.lbound != cvar->t.i.ubound) {

			if ((int_param < cvar->t.i.lbound) ||
			    (int_param > cvar->t.i.ubound)) {
				/* Value is outside of cvar bounds */
				CVAR_ErrorState = CVERR_BOUND;
				return R_FAILURE;
			}
		}

		*(cvar->t.i.var_p) = (cv_int_t) int_param;

#ifdef R_CVAR_TRACE
		printf("Set cvar to value %ld.\n", int_param);
#endif

		break;

	case R_CVAR_UINT:

		if (*r_value == '-') {
			CVAR_ErrorState = CVERR_SIGN;
			return R_FAILURE;
		}

		uint_param = strtoul(r_value, &end_p, 10);

		if (uint_param == ULONG_MAX) {
			CVAR_ErrorState = CVERR_PARSEOVERFLOW;
			return R_FAILURE;
		}

		scan_len = end_p - r_value;

		if (uint_param == 0) {

			if (!scan_len || r_value[scan_len - 1] != '0') {
				/* strtol() returned 0, but string isn't "0". Invalid. */
				CVAR_ErrorState = CVERR_INVALID;
				return R_FAILURE;
			}
		}

		if (scan_len != r_value_len) {
			/* Entire string wasn't converted...Invalid */
			CVAR_ErrorState = CVERR_INVALID;
			return R_FAILURE;
		}

		if (uint_param > CV_UINTMAX) {
			/* Overflows destination type */
			CVAR_ErrorState = CVERR_DESTOVERFLOW;
			return R_FAILURE;
		}

		/* Ignore bounds if equal */
		if (cvar->t.ui.lbound != cvar->t.ui.ubound) {

			if ((uint_param < cvar->t.ui.lbound) ||
			    (uint_param > cvar->t.ui.ubound)) {
				/* Value is outside cvar bounds */
				CVAR_ErrorState = CVERR_BOUND;
				return R_FAILURE;
			}
		}

		*(cvar->t.ui.var_p) = (cv_uint_t) uint_param;

#ifdef R_CVAR_TRACE
		printf("Set cvar to value %lu.\n", uint_param);
#endif

		break;

	case R_CVAR_FLOAT:

		CVAR_ErrorState = CVERR_NOTIMPL;
		return R_FAILURE;
		break;

	case R_CVAR_STRING:

		if (strrchr(r_value, '\"') != NULL) {
			CVAR_ErrorState = CVERR_STRING;
			return R_FAILURE;
		}

		strncpy(cvar->t.s.var_str, r_value, cvar->t.s.ubound);
		if (cvar->t.s.ubound < r_value_len) {
			cvar->t.s.var_str[cvar->t.s.ubound] = 0;
		}
#ifdef R_CVAR_TRACE
		printf("Set cvar to value \"%s\".\n", cvar->t.s.var_str);
#endif

		break;

	default:

		CVAR_ErrorState = CVERR_TYPE;
		return R_FAILURE;
		break;
	}

	CVAR_ErrorState = CVERR_NONE;
	return R_SUCCESS;
}

R_CVAR_P CVAR_Find(const char *var_str)
/****************************************************************************\
 Given a cvar name this function returns a pointer to the appropriate 
 cvar structure or NULL if no match was found.
\****************************************************************************/
{

	R_CVAR *walk_ptr;
	int hash;

	hash = CVAR_HashString(var_str);

#ifdef R_CVAR_TRACE
	printf("Performing lookup on hash bucket %d.\n", hash);
#endif

	walk_ptr = CVHashTbl[hash];

	while (walk_ptr != NULL) {

		if (strcmp(var_str, walk_ptr->name) == 0) {
			return walk_ptr;
		}

		walk_ptr = walk_ptr->next;
	}

	return NULL;
}

int CVAR_IsFunc(R_CVAR_P cvar_func)
{

	if (cvar_func->type == R_CVAR_FUNC)
		return 1;
	else
		return 0;
}

int
CVAR_RegisterFunc(cv_func_t func,
    const char *func_name,
    const char *func_argstr, uint flags, int min_args, int max_args)
/****************************************************************************\
 Registers a console function 'cvar' 
 (could think of a better place to put these...?)
\****************************************************************************/
{

	R_CVAR new_cvar;
	int hash;

	new_cvar.name = func_name;
	new_cvar.type = R_CVAR_FUNC;
	new_cvar.section = NULL;
	new_cvar.flags = flags;

	new_cvar.t.func.func_p = func;
	new_cvar.t.func.func_argstr = func_argstr;
	new_cvar.t.func.min_args = min_args;
	new_cvar.t.func.max_args = max_args;
	hash = CVAR_HashString(func_name);

#ifdef R_CVAR_TRACE
	printf("Added FUNC cvar to hash bucket %d.\n", hash);
#endif

	return CVAR_Add(hash, &new_cvar);
}

int
CVAR_Register_I(cv_int_t * var_p,
    const char *var_name,
    const char *section, uint flags, cv_int_t lbound, cv_int_t ubound)
/****************************************************************************\
 Registers an integer type cvar.
\****************************************************************************/
{

	R_CVAR new_cvar;
	int hash;

	new_cvar.name = var_name;
	new_cvar.type = R_CVAR_INT;
	new_cvar.section = section;
	new_cvar.flags = flags;

	new_cvar.t.i.var_p = var_p;

	new_cvar.t.i.lbound = lbound;
	new_cvar.t.i.ubound = ubound;

	hash = CVAR_HashString(var_name);

#ifdef R_CVAR_TRACE
	printf("Added INT cvar to hash bucket %d.\n", hash);
#endif

	return CVAR_Add(hash, &new_cvar);
}

int
CVAR_Register_UI(cv_uint_t * var_p,
    const char *var_name,
    const char *section, uint flags, cv_uint_t lbound, cv_uint_t ubound)
/****************************************************************************\ 
 Registers an unsigned integer type cvar.
\****************************************************************************/
{

	R_CVAR new_cvar;
	int hash;

	new_cvar.name = var_name;
	new_cvar.type = R_CVAR_UINT;
	new_cvar.section = section;
	new_cvar.flags = flags;

	new_cvar.t.ui.var_p = var_p;

	new_cvar.t.ui.lbound = lbound;
	new_cvar.t.ui.ubound = ubound;

	hash = CVAR_HashString(var_name);

#ifdef R_CVAR_TRACE
	printf("Added UNSIGNED INT ccvar to hash bucket %d.\n", hash);
#endif

	return CVAR_Add(hash, &new_cvar);
}

int
CVAR_Register_F(cv_float_t * var_p,
    const char *var_name,
    const char *section, uint flags, cv_float_t lbound, cv_float_t ubound)
/****************************************************************************\
 Registers a floating point type cvar.
\****************************************************************************/
{

	R_CVAR new_cvar;
	int hash;

	new_cvar.name = var_name;
	new_cvar.type = R_CVAR_FLOAT;
	new_cvar.section = section;
	new_cvar.flags = flags;

	new_cvar.t.f.var_p = var_p;

	new_cvar.t.f.lbound = lbound;
	new_cvar.t.f.ubound = ubound;

	hash = CVAR_HashString(var_name);

#ifdef R_CVAR_TRACE
	printf("Added FLOAT cvar to hash bucket %d.\n", hash);
#endif

	return CVAR_Add(hash, &new_cvar);
}

int
CVAR_Register_S(cv_char_t * var_str,
    const char *var_name, const char *section, uint flags, int ubound)
/****************************************************************************\
 Registers a string type cvar. Storage must be provided in var_p for 'ubound'
 characters plus 1 for NUL char. 
\****************************************************************************/
{

	R_CVAR new_cvar;
	int hash;

	new_cvar.name = var_name;
	new_cvar.type = R_CVAR_STRING;
	new_cvar.section = section;
	new_cvar.flags = flags;

	new_cvar.t.s.var_str = var_str;
	new_cvar.t.s.ubound = ubound;

	hash = CVAR_HashString(var_name);

#ifdef R_CVAR_TRACE
	printf("Added UNSIGNED INT var to hash bucket %d.\n", hash);
#endif

	return CVAR_Add(hash, &new_cvar);
}

int CVAR_Print(R_CVAR_P con_cvar)
/****************************************************************************\
 Displays the value and type of the given cvar to the console.
\****************************************************************************/
{

	switch (con_cvar->type) {

	case R_CVAR_INT:
		CON_Print("\"%s\"(i) = %d",
		    con_cvar->name, *(con_cvar->t.i.var_p));
		break;

	case R_CVAR_UINT:
		CON_Print("\"%s\"(ui) = %u",
		    con_cvar->name, *(con_cvar->t.ui.var_p));
		break;

	case R_CVAR_FLOAT:
		CON_Print("\"%s\"(ui) = %f",
		    con_cvar->name, *(con_cvar->t.f.var_p));
		break;

	case R_CVAR_STRING:
		CON_Print("\"%s\"(s) = \"%s\"", con_cvar->name,
		    con_cvar->t.s.var_str);
		break;

	case R_CVAR_FUNC:
		if (con_cvar->t.func.func_argstr) {
			CON_Print("\"%s\"(func) Args: %s", con_cvar->name,
			    con_cvar->t.func.func_argstr);
		} else {
			CON_Print("\"%s\"(func) No arguments.",
			    con_cvar->name);
		}
		break;

	default:
		CON_Print("Invalid variable type.\n");
		break;
	}

	return R_SUCCESS;
}

} // End of namespace Saga