aboutsummaryrefslogtreecommitdiff
path: root/src/libparsec/con_arg.cpp
blob: 83d97d997f43bd81b628e1cd2d08babd8d890a90 (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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
/*
 * PARSEC - Argument Parsing
 *
 * $Author: uberlinuxguy $ - $Date: 2004/09/26 03:43:44 $
 *
 * Orginally written by:
 *   Copyright (c) Markus Hadwiger     <msh@parsec.org>   1996-2000
 *
 *  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
 */ 

// C library
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// compilation flags/debug support
#include "config.h"
#include "debug.h"

// general definitions
#include "general.h"
#include "objstruc.h"

// global externals
#include "globals.h"

// local module header
#include "con_arg.h"

// proprietary module headers
#ifdef PARSEC_SERVER
	#include "con_int_sv.h"
	#include "con_main_sv.h"
#else // !PARSEC_SERVER
	#include "con_int.h"
	#include "con_main.h"
#endif // !PARSEC_SERVER

#include "obj_clas.h"



// generic string paste area --------------------------------------------------
//
#define PASTE_STR_LEN 255
static char paste_str[ PASTE_STR_LEN + 1 ];


// string constants -----------------------------------------------------------
//
static char value_missing[]			= "value missing.";
static char unknown_key[]			= "unknown key.";
static char invalid_value[]			= "invalid value syntax.";
static char disallowed_key[]		= "key not allowed.";
static char int_list_invalid[]		= "int list invalid.";
static char int_list_too_long[]		= "int list too long.";
static char int_list_too_short[]	= "int list too short.";
static char float_list_invalid[]	= "float list invalid.";
static char float_list_too_long[]	= "float list too long.";
static char float_list_too_short[]	= "float list too short.";
static char flag_value_invalid[]	= "invalid flag specified.";
static char invalid_class[] 		= "invalid object class identifier.";
static char object_spec_needed[]	= "object class must be specified.";
static char too_many_strings[]		= "exactly one string must be supplied.";
static char string_arg_too_long[]	= "string too long.";


// scan out all values to specified keys (key/value pairs) --------------------
//
int ScanKeyValuePairs( key_value_s *table, char *kvstr )
{
	ASSERT( table != NULL );

	//NOTE:
	// kvstr may be NULL to continue parsing with
	// strtok( NULL, " " ).

	// reset all values
	int keyno = 0;
	for ( keyno = 0; table[ keyno ].key; keyno++ )
		table[ keyno ].value = NULL;

	// scan all supplied key/value pairs
	char *key;
	while ( ( key = strtok( kvstr, " " ) ) != NULL ) {
		// continue in same string
		kvstr = NULL;
		// check all keys in table
		for ( keyno = 0; table[ keyno ].key; keyno++ ) {
			if ( strcmp( table[ keyno ].key, key ) == 0 ) {
				if ( table[ keyno ].flags & KEYVALFLAG_DISALLOW ) {
					CON_AddLine( disallowed_key );
					return FALSE;
				}
				char *value = strtok( NULL, " " );
				if ( value == NULL ) {
					CON_AddLine( value_missing );
					return FALSE;
				}
				if ( table[ keyno ].flags & KEYVALFLAG_PARENTHESIZE ) {
					value = GetParenthesizedName( value );
					if ( value == NULL ) {
						CON_AddLine( invalid_value );
						return FALSE;
					}
				}
				if ( table[ keyno ].flags & KEYVALFLAG_IGNORE ) {
					sprintf( paste_str, "key %s ignored.", table[ keyno ].key );
					CON_AddLine( paste_str );
				} else {
					table[ keyno ].value = value;
				}
				break;
			}
		}
		if ( table[ keyno ].key == NULL ) {
			CON_AddLine( unknown_key );
			return FALSE;
		}
	}

	// check for mandatory keys
	for ( keyno = 0; table[ keyno ].key; keyno++ ) {
		if ( table[ keyno ].flags & KEYVALFLAG_MANDATORY ) {
			if ( table[ keyno ].value == NULL ) {
				sprintf( paste_str, "mandatory key %s missing.", table[ keyno ].key );
				CON_AddLine( paste_str );
				return FALSE;
			}
		}
	}

	return TRUE;
}


// convert integer value of key/value pair to int -----------------------------
//
int ScanKeyValueInt( key_value_s *keyval, int *ival )
{
	//NOTE:
	// returns -1 if value was invalid.
	// returns  0 if value was missing.
	// returns  1 if value was ok.

	ASSERT( keyval != NULL );
	ASSERT( ival != NULL );

	// missing value alters nothing
	if ( keyval->value == NULL ) {
		return 0;
	}

	char *errpart;
	int32 iparam = (int32) strtol( keyval->value, &errpart, 10 );

	// invalid value returns error
	if ( *errpart != 0 ) {
		return -1;
	}

	*ival = iparam;

	return 1;
}


// convert integer value list of key/value pair to int array ------------------
//
int ScanKeyValueIntList( key_value_s *keyval, int *ilist, int minlen, int maxlen )
{
	ASSERT( keyval != NULL );
	ASSERT( ilist != NULL );
	ASSERT( minlen > 0 );
	ASSERT( minlen <= maxlen );

	char *liststr = keyval->value;
	if ( liststr == NULL ) {
		return 0;
	}

	char *istr;
	int cnt = 0;
	for ( cnt = 0; (istr = strtok( liststr, " " )); cnt++ ) {

		liststr = NULL;

		if ( cnt >= maxlen ) {
			CON_AddLine( int_list_too_long );
			return 0;
		}

		char *errpart;
		int iparam1 = -1;

		// check for range specification like "50-70"
		char *checkstr = istr;
		while ( ( *checkstr >= '0' ) && ( *checkstr <= '9' ) )
			checkstr++;
		if ( ( *checkstr == '-' ) && ( checkstr != istr ) ) {
			iparam1 = (int) strtol( istr, &errpart, 10 );
			if ( *errpart != '-' ) {
				CON_AddLine( int_list_invalid );
				return 0;
			}
			istr = &checkstr[ 1 ];
		}

		int iparam2 = (int) strtol( istr, &errpart, 10 );
		if ( *errpart != 0 ) {
			CON_AddLine( int_list_invalid );
			return 0;
		}

		if ( iparam1 != -1 ) {

			// store specified range
			if ( iparam1 <= iparam2 ) {

				if ( ( cnt + iparam2 - iparam1 ) >= maxlen ) {
					CON_AddLine( int_list_too_long );
					return 0;
				}
				cnt += iparam2 - iparam1;
				for ( ; iparam1 <= iparam2; iparam1++ ) {
					*ilist++ = iparam1;
				}

			} else {

				if ( ( cnt + iparam1 - iparam2 ) >= maxlen ) {
					CON_AddLine( int_list_too_long );
					return 0;
				}
				cnt += iparam1 - iparam2;
				for ( ; iparam1 >= iparam2; iparam1-- ) {
					*ilist++ = iparam1;
				}
			}

		} else {

			// store single value
			*ilist++ = iparam2;
		}
	}

	if ( cnt < minlen ) {
		CON_AddLine( int_list_too_short );
		return 0;
	}

	return cnt;
}


// convert bounded integer value list of key/value pair to int array ----------
//
int ScanKeyValueIntListBounded( key_value_s *keyval, int *ilist, int minlen, int maxlen, int minval, int maxval )
{
	ASSERT( keyval != NULL );
	ASSERT( ilist != NULL );
	ASSERT( minlen > 0 );
	ASSERT( minlen <= maxlen );
	ASSERT( minval <= maxval );

	char *liststr = keyval->value;
	if ( liststr == NULL ) {
		return 0;
	}

	char *istr;
	int cnt = 0;
	for ( cnt = 0; (istr = strtok( liststr, " " )); cnt++ ) {

		liststr = NULL;

		if ( cnt >= maxlen ) {
			CON_AddLine( int_list_too_long );
			return 0;
		}

		char *errpart;
		int iparam1 = -1;

		// check for range specification like "50-70"
		char *checkstr = istr;
		while ( ( *checkstr >= '0' ) && ( *checkstr <= '9' ) )
			checkstr++;
		if ( ( *checkstr == '-' ) && ( checkstr != istr ) ) {
			iparam1 = (int) strtol( istr, &errpart, 10 );
			if ( *errpart != '-' ) {
				CON_AddLine( int_list_invalid );
				return 0;
			}
			istr = &checkstr[ 1 ];
		}

		int iparam2 = (int) strtol( istr, &errpart, 10 );
		if ( *errpart != 0 ) {
			CON_AddLine( int_list_invalid );
			return 0;
		}

		if ( ( iparam2 < minval ) || ( iparam2 > maxval ) ) {
			CON_AddLine( int_list_invalid );
			return 0;
		}

		if ( iparam1 != -1 ) {

			if ( ( iparam1 < minval ) || ( iparam1 > maxval ) ) {
				CON_AddLine( int_list_invalid );
				return 0;
			}

			// store specified range
			if ( iparam1 <= iparam2 ) {

				if ( ( cnt + iparam2 - iparam1 ) >= maxlen ) {
					CON_AddLine( int_list_too_long );
					return 0;
				}
				cnt += iparam2 - iparam1;
				for ( ; iparam1 <= iparam2; iparam1++ ) {
					*ilist++ = iparam1;
				}

			} else {

				if ( ( cnt + iparam1 - iparam2 ) >= maxlen ) {
					CON_AddLine( int_list_too_long );
					return 0;
				}
				cnt += iparam1 - iparam2;
				for ( ; iparam1 >= iparam2; iparam1-- ) {
					*ilist++ = iparam1;
				}
			}

		} else {

			// store single value
			*ilist++ = iparam2;
		}
	}

	if ( cnt < minlen ) {
		CON_AddLine( int_list_too_short );
		return 0;
	}

	return cnt;
}


// convert float value of key/value pair to float -----------------------------
//
int ScanKeyValueFloat( key_value_s *keyval, float *fval )
{
	//NOTE:
	// returns -1 if value was invalid.
	// returns  0 if value was missing.
	// returns  1 if value was ok.

	ASSERT( keyval != NULL );
	ASSERT( fval != NULL );

	// missing value alters nothing
	if ( keyval->value == NULL ) {
		return 0;
	}

	char *errpart;
	double fparam = strtod( keyval->value, &errpart );

	// invalid value returns error
	if ( *errpart != 0 ) {
		return -1;
	}

	*fval = (float)fparam;

	return 1;
}


// convert float value list of key/value pair to float array ------------------
//
int ScanKeyValueFloatList( key_value_s *keyval, float *flist, int minlen, int maxlen )
{
	ASSERT( keyval != NULL );
	ASSERT( flist != NULL );
	ASSERT( minlen > 0 );
	ASSERT( minlen <= maxlen );

	char *liststr = keyval->value;
	if ( liststr == NULL ) {
		return 0;
	}

	char *fstr;
	int cnt = 0;
	for ( cnt = 0; (fstr = strtok( liststr, " " )); cnt++ ) {

		liststr = NULL;

		if ( cnt >= maxlen ) {
			CON_AddLine( float_list_too_long );
			return 0;
		}

		char *errpart;
		double fparam = strtod( fstr, &errpart );
		if ( *errpart != 0 ) {
			CON_AddLine( float_list_invalid );
			return 0;
		}

		*flist++ = (float)fparam;
	}

	if ( cnt < minlen ) {
		CON_AddLine( float_list_too_short );
		return 0;
	}

	return cnt;
}


// convert bounded float value list of key/value pair to float array ----------
//
int ScanKeyValueFloatListBounded( key_value_s *keyval, float *flist, int minlen, int maxlen, float minval, float maxval )
{
	ASSERT( keyval != NULL );
	ASSERT( flist != NULL );
	ASSERT( minlen > 0 );
	ASSERT( minlen <= maxlen );
	ASSERT( minval <= maxval );

	char *liststr = keyval->value;
	if ( liststr == NULL ) {
		return 0;
	}

	char *fstr;
	int cnt = 0;
	for ( cnt = 0; (fstr = strtok( liststr, " " )); cnt++ ) {

		liststr = NULL;

		if ( cnt >= maxlen ) {
			CON_AddLine( float_list_too_long );
			return 0;
		}

		char *errpart;
		double fparam = strtod( fstr, &errpart );
		if ( *errpart != 0 ) {
			CON_AddLine( float_list_invalid );
			return 0;
		}

		if ( ( fparam < minval ) || ( fparam > maxval ) ) {
			CON_AddLine( float_list_invalid );
			return 0;
		}

		*flist++ = (float)fparam;
	}

	if ( cnt < minlen ) {
		CON_AddLine( float_list_too_short );
		return 0;
	}

	return cnt;
}


// convert flag value list of key/value pair to single flagword ---------------
//
int ScanKeyValueFlagList( key_value_s *keyval, dword *flagword, flag_map_s *flagmap )
{
	ASSERT( keyval != NULL );
	ASSERT( flagword != NULL );
	ASSERT( flagmap != NULL );

	char *liststr = keyval->value;
	if ( liststr == NULL ) {
		return 0;
	}

	char *fstr;
	int cnt = 0;
	for ( cnt = 0; (fstr = strtok( liststr, " " )); cnt++ ) {

		liststr = NULL;

		// look for flag spec match
		flag_map_s *map = NULL;
		for ( map = flagmap; map->name != NULL; map++ ) {
			if ( stricmp( fstr, map->name ) == 0 ) {
				break;
			}
		}
		if ( map->name == NULL ) {
			CON_AddLine( flag_value_invalid );
			return 0;
		}

		// set corresponding bit
		*flagword |= map->value;
	}

	return cnt;
}


// convert class spec (either class name or id) of key/value pair to id -------
//
dword ScanKeyValueObjClass( key_value_s *keyval, int keyclass, int keyid )
{
	ASSERT( keyval != NULL );

	dword objclass = CLASS_ID_INVALID;

	// if name was specified try to get	class id via name lookup
	char *classname = keyval[ keyclass ].value;
	if ( classname != NULL ) {
		objclass = OBJ_FetchObjectClassId( classname );
		if ( objclass == CLASS_ID_INVALID ) {
			CON_AddLine( invalid_class );
			return CLASS_ID_INVALID;
		}
	}

	// if the name was not found, try the id
	if ( objclass == CLASS_ID_INVALID ) {
		if ( ScanKeyValueInt( &keyval[ keyid ], (int*)&objclass ) < 0 ) {
			CON_AddLine( invalid_class );
			return CLASS_ID_INVALID;
		}
	}

	// if neither name nor id was specified print error msg
	if ( objclass == CLASS_ID_INVALID ) {
		CON_AddLine( object_spec_needed );
		return CLASS_ID_INVALID;
	}

	if ( objclass >= ( (dword)NumObjClasses ) ) {
		CON_AddLine( invalid_class );
		return CLASS_ID_INVALID;
	}

	return objclass;
}


// parse name that may be parenthesized to allow inclusion of whitespace ------
//
char *GetParenthesizedName( char *name )
{
	ASSERT( name != NULL );

	if ( *name == '(' ) {

		// skip leftmost parenthesis
		name++;

		int opened = 1;
		int closed = 0;

		// concatenate fields
		char *nextfield = name;
		for ( ;; ) {

			// count enclosed parentheses
			for ( ; *nextfield != 0; nextfield++ ) {
				if ( *nextfield == '(' )
					opened++;
				else if ( *nextfield == ')' )
					closed++;
			}
			// check for rightmost parenthesis
			if ( *( nextfield - 1 ) == ')' ) {
				if ( closed == opened ) {
					// strip rightmost parenthesis
					*( nextfield - 1 ) = 0;
					return name;
				}
			}

			// replace '\0' by ' '
			*nextfield = ' ';

			// fetch next field
			nextfield = strtok( NULL, " " );
			if ( nextfield == NULL )
				return NULL;
		}
	}

	return name;
}


// get int parameter from behind a command string -----------------------------
//
int GetIntBehindCommand( char *cstr, int32 *iarg, int base, int paramoptional )
{
	//NOTE:
	// this function returns TRUE only if the int parameter
	// could be parsed correctly. otherwise an error message
	// will be displayed and FALSE returned. this implicates
	// that quiet syntax error detection is not possible.
	// (exception: missing parameter if it is optional. in
	// this case paramoptional will be returned to the caller
	// who must ensure to react correctly.)

	ASSERT( cstr != NULL );
	ASSERT( iarg != NULL );

	// check if there is a space between command and argument
	if ( ( *cstr != ' ' ) && ( *cstr != 0 ) ) {
		CON_AddLine( unknown_command );
		return FALSE;
	}

	// split off first token (int parameter)
	char *istr = strtok( cstr, " " );

	if ( istr == NULL ) {
		if ( !paramoptional )
			CON_AddLine( arg_missing );
		return paramoptional;
	}

	// check if more than one parameter
	if ( strtok( NULL, " " ) != NULL ) {
		CON_AddLine( too_many_args );
		return FALSE;
	}

	// convert int parameter
	char *errpart;
	int32 iparam = (int32) strtol( istr, &errpart, base );

	if ( *errpart != 0 ) {
		CON_AddLine( invalid_arg );
		return FALSE;
	}

	*iarg = iparam;
	return TRUE;
}


// get argument string that has to be separated with at least one space -------
//
const char *GetStringBehindCommand( char *scan, int paramoptional )
{
	//NOTE:
	// this function returns non-NULL only if the string parameter
	// could be parsed correctly. otherwise an error message
	// will be displayed and NULL returned. this implicates
	// that quiet syntax error detection is not possible.
	// (exception: missing parameter if it is optional.)

	//NOTE:
	// if the parameter is optional this function
	// will return an empty string ("").

	ASSERT( scan != NULL );

	// check if there is a space between command and argument
	if ( ( *scan != 0 ) && ( *scan != ' ' ) ) {
		CON_AddLine( unknown_command );
		return NULL;
	}

	// eat whitespace
	while ( *scan == ' ' )
		scan++;

	// no argument found?
	if ( *scan == 0 ) {
		if ( paramoptional ) {
			// return empty string
			return "";
		} else {
			// return error
			CON_AddLine( arg_missing );
			return NULL;
		}
	}

	// cut off trailing whitespace
	char *start = scan;
	while ( *scan != 0 )
		scan++;
	while ( *--scan == ' ' )
		{}
	*( scan + 1 ) = 0;

	// return isolated part (may contain whitespace in the middle!)
	return start;
}


// generic string set function ------------------------------------------------
//
int SetSingleStringCommand( char *cstr, char *dst, int dstmaxlen )
{
	ASSERT( cstr != NULL );
	ASSERT( dst != NULL );
	ASSERT( dstmaxlen > 0 );

	// check if there is a space between command and argument
	if ( ( *cstr != ' ' ) && ( *cstr != 0 ) ) {
		CON_AddLine( unknown_command );
		return FALSE;
	}

	char *name = strtok( cstr, " " );

	if ( name == NULL ) {
		CON_AddLine( dst );
		return FALSE;
	}

	if ( strtok( NULL, " " ) != NULL ) {
		CON_AddLine( too_many_strings );
		return FALSE;
	}

	if ( ( (int)strlen( name ) ) > dstmaxlen ) {
		CON_AddLine( string_arg_too_long );
		return FALSE;
	}

	strcpy( dst, name );
	return TRUE;
}


// query/retrieve single int argument -----------------------------------------
//
char *QueryIntArgument( const char *query, int *arg )
{
	//NOTE:
	// query may be NULL, indicating that the variable
	// may be queried by not supplying any argument.

	ASSERT( arg != NULL );

	char *scan = strtok( NULL, " " );
	if ( scan != NULL ) {
		if ( strtok( NULL, " " ) == NULL ) {
			return scan;
		} else {
			CON_AddLine( too_many_args );
		}
	} else if ( query != NULL ) {
		sprintf( paste_str, query, *arg );
		CON_AddLine( paste_str );
	} else {
		CON_AddLine( arg_missing );
	}

	return NULL;
}


// query/retrieve single int argument not using strtok( NULL, ... ) -----------
//
char *QueryIntArgumentEx( char *params, const char *query, int *arg )
{
	//NOTE:
	// query may be NULL, indicating that the variable
	// may be queried by not supplying any argument.

	ASSERT( params != NULL );
	ASSERT( arg != NULL );

	char *scan = strtok( params, " " );
	if ( scan != NULL ) {
		if ( strtok( NULL, " " ) == NULL ) {
			return scan;
		} else {
			CON_AddLine( too_many_args );
		}
	} else if ( query != NULL ) {
		sprintf( paste_str, query, *arg );
		CON_AddLine( paste_str );
	} else {
		CON_AddLine( arg_missing );
	}

	return NULL;
}


// query/retrieve single float argument ---------------------------------------
//
char *QueryFltArgument( const char *query, float *arg )
{
	//NOTE:
	// query may be NULL, indicating that the variable
	// may be queried by not supplying any argument.

	ASSERT( arg != NULL );

	char *scan = strtok( NULL, " " );
	if ( scan != NULL ) {
		if ( strtok( NULL, " " ) == NULL ) {
			return scan;
		} else {
			CON_AddLine( too_many_args );
		}
	} else if ( query != NULL ) {
		sprintf( paste_str, query, *arg );
		CON_AddLine( paste_str );
	} else {
		CON_AddLine( arg_missing );
	}

	return NULL;
}


// query/retrieve single float argument not using strtok( NULL, ... ) ---------
//
char *QueryFltArgumentEx( char *params, const char *query, float *arg )
{
	//NOTE:
	// query may be NULL, indicating that the variable
	// may be queried by not supplying any argument.

	ASSERT( params != NULL );
	ASSERT( arg != NULL );

	char *scan = strtok( params, " " );
	if ( scan != NULL ) {
		if ( strtok( NULL, " " ) == NULL ) {
			return scan;
		} else {
			CON_AddLine( too_many_args );
		}
	} else if ( query != NULL ) {
		sprintf( paste_str, query, *arg );
		CON_AddLine( paste_str );
	} else {
		CON_AddLine( arg_missing );
	}

	return NULL;
}


// check command that alters an int value -------------------------------------
//
int CheckSetIntArgument( const char *query, const char *scan, const char *command, int *arg )
{
	//NOTE:
	// query may be NULL, indicating that the variable
	// may be queried by not supplying any argument.

	ASSERT( scan != NULL );
	ASSERT( command != NULL );
	ASSERT( arg != NULL );

	if ( strcmp( scan, command ) == 0 ) {
		if ( (scan = QueryIntArgument( query, arg )) ) {
			char *errpart;
			int sval = (int) strtol( scan, &errpart, int_calc_base );
			if ( *errpart == 0 ) {
				*arg = sval;
			} else {
				CON_AddLine( invalid_arg );
			}
		}
		return TRUE;
	} else {
		return FALSE;
	}
}


// check command that alters a float value ------------------------------------
//
int CheckSetFltArgument( const char *query, const char *scan, const char *command, float *arg )
{
	//NOTE:
	// query may be NULL, indicating that the variable
	// may be queried by not supplying any argument.

	ASSERT( scan != NULL );
	ASSERT( command != NULL );
	ASSERT( arg != NULL );

	if ( strcmp( scan, command ) == 0 ) {
		if ( (scan = QueryFltArgument( query, arg )) ) {
			char *errpart;
			float sval = (float)strtod( scan, &errpart );
			if ( *errpart == 0 ) {
				*arg = sval;
			} else {
				CON_AddLine( invalid_arg );
			}
		}
		return TRUE;
	} else {
		return FALSE;
	}
}


// check command that alters an int value with guaranteed bounds --------------
//
int CheckSetIntArgBounded( const char *query, const char *scan, char *command, int *arg, int bmin, int bmax, void (*func)() )
{
	//NOTE:
	// this function accepts delta arguments. instead of
	// an absolute value ++val or --val may be supplied
	// to specify a value that should be added to or
	// subtracted from the current value, respectively.

	//NOTE:
	// query may be NULL, indicating that the variable
	// may be queried by not supplying any argument.

	ASSERT( scan != NULL );
	ASSERT( command != NULL );
	ASSERT( arg != NULL );
	ASSERT( bmin <= bmax );

	if ( strcmp( scan, command ) == 0 ) {
		if ( (scan = QueryIntArgument( query, arg )) ) {

			// determine if delta modification (++/--)
			int delta = 0;
			if ( ( scan[ 0 ] == '+' ) && ( scan[ 1 ] == '+' ) )
				delta = 1;
			else if ( ( scan[ 0 ] == '-' ) && ( scan[ 1 ] == '-' )  )
				delta = -1;
			if ( delta != 0 )
				scan += 2;

			if ( *scan != 0 ) {
				char *errpart;
				int sval = (int) strtol( scan, &errpart, int_calc_base );
				if ( *errpart == 0 ) {
					if ( delta != 0 )
						sval = *arg + sval * delta;
					if ( sval >= bmin && sval <= bmax ) {
						*arg = sval;
						if ( func != NULL )
							(*func)();
					} else {
						CON_AddLine( range_error );
					}
				} else {
					CON_AddLine( invalid_arg );
				}
			} else {
				CON_AddLine( invalid_arg );
			}
		}
		return TRUE;
	} else {
		return FALSE;
	}
}


// check commands that manipulate int array contents --------------------------
//
int CheckSetIntArray( const char *query, char *scan, char *comstub, int *array, int numelements )
{
	//NOTE:
	// query may be NULL, indicating that the variable
	// may be queried by not supplying any argument.

	ASSERT( scan != NULL );
	ASSERT( comstub != NULL );
	ASSERT( array != NULL );
	ASSERT( numelements > 0 );

	size_t cslen = strlen( comstub );

	if ( strncmp( scan, comstub, cslen ) == 0 ) {

		int indx;
		int ndig = 0;

		if ( ( strlen( scan ) == cslen + 2 ) &&
			 isdigit( scan[ cslen + 0 ] ) &&
			 isdigit( scan[ cslen + 1 ] ) ) {
			ndig = 2;
			indx = ( scan[ cslen ] - '0' ) * 10 + scan[ cslen + 1 ] - '0';
		} else if ( ( strlen( scan ) == cslen + 3 ) &&
			 isdigit( scan[ cslen + 0 ] ) &&
			 isdigit( scan[ cslen + 1 ] ) &&
			 isdigit( scan[ cslen + 2 ] ) ) {
			ndig = 3;
			indx = ( scan[ cslen ] - '0' ) * 100 + ( scan[ cslen + 1 ] - '0' ) * 10 + scan[ cslen + 2 ] - '0';
		}

		if ( ndig > 0 ) {
			if ( indx < numelements ) {
				int *arg = array + indx;
				if ( (scan = QueryIntArgument( query, arg )) ) {
					char *errpart;
					int sval = (int) strtol( scan, &errpart, int_calc_base );
					if ( *errpart == 0 ) {
						*arg = sval;
					} else {
						CON_AddLine( invalid_arg );
					}
				}
				return TRUE;
			} else {
				return FALSE;
			}
		} else {
			return FALSE;
		}
	} else {
		return FALSE;
	}
}


// check argument that alters a single string ---------------------------------
//
int CheckSetStrArgument( int query, char *scan, char *command, char *string, int maxlen )
{
	ASSERT( ( query == FALSE ) || ( query == TRUE ) );
	ASSERT( scan != NULL );
	ASSERT( command != NULL );
	ASSERT( string != NULL );
	ASSERT( maxlen > 0 );

	if ( strcmp( scan, command ) == 0 ) {
		if ( (scan = strtok( NULL, " " )) ) {
			if ( !strtok( NULL, " " ) ) {
				strncpy( string, scan, maxlen );
				string[ maxlen ] = 0;
			} else {
				CON_AddLine( too_many_args );
			}
		} else {
			if ( query ) {
				CON_AddLine( string );
			} else {
				CON_AddLine( arg_missing );
			}
		}
		return TRUE;
	} else {
		return FALSE;
	}
}