1#include "master.h"
2
3int		com_argc;
4char	**com_argv;
5
6/*
7================
8COM_CheckParm
9
10Returns the position (1 to argc-1) in the program's argument list
11where the given parameter apears, or 0 if not present
12================
13*/
14int COM_CheckParm (char *parm)
15{
16	int		i;
17
18	for (i=1 ; i<com_argc ; i++)
19	{
20		if (!com_argv[i])
21			continue;		// NEXTSTEP sometimes clears appkit vars.
22		if (!strcmp (parm,com_argv[i]))
23			return i;
24	}
25
26	return 0;
27}
28
29char	*AdrToString (netadr_t a)
30{
31	static	char	s[64];
32
33	sprintf (s, "%i.%i.%i.%i:%i",
34		a.sin_addr.S_un.S_un_b.s_b1,
35		a.sin_addr.S_un.S_un_b.s_b2,
36		a.sin_addr.S_un.S_un_b.s_b3,
37		a.sin_addr.S_un.S_un_b.s_b4,
38		ntohs(a.sin_port));
39
40	return s;
41}
42
43netadr_t	StringToAdr (char *s)
44{
45	netadr_t		a;
46	int			b1, b2, b3, b4, p;
47
48	b1 = b2 = b3 = b4 = p = 0;
49	sscanf (s, "%i.%i.%i.%i:%i", &b1, &b2, &b3, &b4, &p);
50
51	a.sin_addr.S_un.S_un_b.s_b1 = b1;
52	a.sin_addr.S_un.S_un_b.s_b2 = b2;
53	a.sin_addr.S_un.S_un_b.s_b3 = b3;
54	a.sin_addr.S_un.S_un_b.s_b4 = b4;
55	a.sin_port = ntohs(p);
56
57	return a;
58}
59
60qboolean	CompareAdr (netadr_t a, netadr_t b)
61{
62	if (a.sin_addr.s_addr == b.sin_addr.s_addr
63	&& a.sin_port == b.sin_port)
64		return true;
65	return false;
66}
67
68
69void	mprintf (char *msg, ...)
70{
71	va_list	argptr;
72
73	va_start (argptr, msg);
74	vprintf (msg, argptr);
75	vfprintf (logfile, msg, argptr);
76	va_end (argptr);
77
78}
79
80int	msg_readcount;
81char	*MSG_ReadString (void)
82{
83	char	*start;
84
85	start = packet_data + msg_readcount;
86
87	for ( ; msg_readcount < packet_length ; msg_readcount++)
88		if (packet_data[msg_readcount] == '\n'
89		|| packet_data[msg_readcount] == 0)
90			break;
91
92	packet_data[msg_readcount] = 0;
93	msg_readcount++;
94
95	return start;
96}
97
98/*
99=====================================================================
100
101  COMMAND LINES
102
103=====================================================================
104*/
105
106#define	MAX_ARGS	32
107#define	MAX_ARG_LENGTH	1024
108int		cmd_argc;
109char	cmd_argv[MAX_ARGS][MAX_ARG_LENGTH];
110
111char		com_token[1024];
112/*
113==============
114COM_Parse
115
116Parse a token out of a string
117==============
118*/
119char *COM_Parse (char *data)
120{
121	int		c;
122	int		len;
123
124	len = 0;
125	com_token[0] = 0;
126
127	if (!data)
128		return NULL;
129
130// skip whitespace
131skipwhite:
132	while ( (c = *data) <= ' ')
133	{
134		if (c == 0)
135			return NULL;			// end of file;
136		data++;
137	}
138
139// skip // comments
140	if (c=='/' && data[1] == '/')
141	{
142		while (*data && *data != '\n')
143			data++;
144		goto skipwhite;
145	}
146
147
148// handle quoted strings specially
149	if (c == '\"')
150	{
151		data++;
152		while (1)
153		{
154			c = *data++;
155			if (c=='\"' || !c)
156			{
157				com_token[len] = 0;
158				return data;
159			}
160			com_token[len] = c;
161			len++;
162		}
163	}
164
165// parse a regular word
166	do
167	{
168		com_token[len] = c;
169		data++;
170		len++;
171		c = *data;
172	} while (c>32);
173
174	com_token[len] = 0;
175	return data;
176}
177
178/*
179============
180Cmd_Argc
181============
182*/
183int		Cmd_Argc (void)
184{
185	return cmd_argc;
186}
187
188/*
189============
190Cmd_Argv
191============
192*/
193char	*Cmd_Argv (int arg)
194{
195	if ( (unsigned)arg >= cmd_argc )
196		return "";
197	return cmd_argv[arg];
198}
199
200/*
201============
202Cmd_TokenizeString
203
204Parses the given string into command line tokens.
205============
206*/
207void Cmd_TokenizeString (char *text)
208{
209	cmd_argc = 0;
210
211	while (1)
212	{
213// skip whitespace up to a /n
214		while (*text && *text <= ' ')
215		{
216			text++;
217		}
218
219		if (!*text)
220			return;
221
222		text = COM_Parse (text);
223		if (!text)
224			return;
225
226		if (cmd_argc < MAX_ARGS)
227		{
228			strcpy (cmd_argv[cmd_argc], com_token);
229			cmd_argc++;
230		}
231	}
232
233}
234
235
236/*
237=====================================================================
238
239  INFO STRINGS
240
241=====================================================================
242*/
243
244/*
245===============
246Info_ValueForKey
247
248Searches the string (userinfo or masterinfo) for the given
249key and returns the associated value, or an empty string.
250===============
251*/
252char *Info_ValueForKey (char *s, char *key)
253{
254	char	pkey[512];
255	static	char value[512];
256	char	*o;
257
258	if (*s == '\\')
259		s++;
260	while (1)
261	{
262		o = pkey;
263		while (*s != '\\')
264		{
265			if (!*s)
266				return "";
267			*o++ = *s++;
268		}
269		*o = 0;
270		s++;
271
272		o = value;
273		while (*s != '\\' && *s)
274		{
275			if (!*s)
276				return "";
277			*o++ = *s++;
278		}
279		*o = 0;
280
281		if (!strcmp (key, pkey) )
282			return value;
283
284		if (!*s)
285			return "";
286		s++;
287	}
288}
289
290void Info_RemoveKey (char *s, char *key)
291{
292	char	*start;
293	char	pkey[512];
294	char	value[512];
295	char	*o;
296
297	if (strstr (key, "\\"))
298	{
299		printf ("Can't use a key with a \\\n");
300		return;
301	}
302
303	while (1)
304	{
305		start = s;
306		if (*s == '\\')
307			s++;
308		o = pkey;
309		while (*s != '\\')
310		{
311			if (!*s)
312				return;
313			*o++ = *s++;
314		}
315		*o = 0;
316		s++;
317
318		o = value;
319		while (*s != '\\' && *s)
320		{
321			if (!*s)
322				return;
323			*o++ = *s++;
324		}
325		*o = 0;
326
327		if (!strcmp (key, pkey) )
328		{
329			strcpy (start, s);	// remove this part
330			return;
331		}
332
333		if (!*s)
334			return;
335	}
336
337}
338
339void Info_RemovePrefixedKeys (char *start, char prefix)
340{
341	char	*s;
342	char	pkey[512];
343	char	value[512];
344	char	*o;
345
346	s = start;
347
348	while (1)
349	{
350		if (*s == '\\')
351			s++;
352		o = pkey;
353		while (*s != '\\')
354		{
355			if (!*s)
356				return;
357			*o++ = *s++;
358		}
359		*o = 0;
360		s++;
361
362		o = value;
363		while (*s != '\\' && *s)
364		{
365			if (!*s)
366				return;
367			*o++ = *s++;
368		}
369		*o = 0;
370
371		if (pkey[0] == prefix)
372		{
373			Info_RemoveKey (start, pkey);
374			s = start;
375		}
376
377		if (!*s)
378			return;
379	}
380
381}
382
383void Info_SetValueForKey (char *s, char *key, char *value)
384{
385	char	new[512];
386
387	if (strstr (key, "\\") || strstr (value, "\\") )
388	{
389		printf ("Can't use keys or values with a \\\n");
390		return;
391	}
392	if (strstr (key, "\"") || strstr (value, "\"") )
393	{
394		printf ("Can't use keys or values with a \\\n");
395		return;
396	}
397
398	Info_RemoveKey (s, key);
399	if (!value || !strlen(value))
400		return;
401
402	sprintf (new, "\\%s\\%s", key, value);
403
404	if (strlen(new) + strlen(s) > MAX_INFO_STRING)
405	{
406		printf ("Info string length exceeded\n");
407		return;
408	}
409	strcat (s, new);
410}
411
412void Info_Print (char *s)
413{
414	char	key[512];
415	char	value[512];
416	char	*o;
417	int		l;
418
419	if (*s == '\\')
420		s++;
421	while (*s)
422	{
423		o = key;
424		while (*s && *s != '\\')
425			*o++ = *s++;
426
427		l = o - key;
428		if (l < 20)
429		{
430			memset (o, ' ', 20-l);
431			key[20] = 0;
432		}
433		else
434			*o = 0;
435		printf ("%s", key);
436
437		if (!*s)
438		{
439			printf ("MISSING VALUE\n");
440			return;
441		}
442
443		o = value;
444		s++;
445		while (*s && *s != '\\')
446			*o++ = *s++;
447		*o = 0;
448
449		if (*s)
450			s++;
451		printf ("%s\n", value);
452	}
453}
454