misc.c 6.3 KB

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