parse.c 20 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  1. /******************************************************************************
  2. * This file is part of TinTin++ *
  3. * *
  4. * Copyright 2004-2019 Igor van den Hoven *
  5. * *
  6. * TinTin++ is free software; you can redistribute it and/or modify *
  7. * it under the terms of the GNU General Public License as published by *
  8. * the Free Software Foundation; either version 3 of the License, or *
  9. * (at your option) any later version. *
  10. * *
  11. * This program is distributed in the hope that it will be useful, *
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  14. * GNU General Public License for more details. *
  15. * *
  16. * *
  17. * You should have received a copy of the GNU General Public License *
  18. * along with TinTin++. If not, see https://www.gnu.org/licenses. *
  19. ******************************************************************************/
  20. /******************************************************************************
  21. * (T)he K(I)cki(N) (T)ickin D(I)kumud Clie(N)t *
  22. * *
  23. * coded by Peter Unold 1992 *
  24. * recoded by Igor van den Hoven 2005 *
  25. ******************************************************************************/
  26. #include "tintin.h"
  27. // whether str1 is an abbreviation of str2
  28. int case_table[256] =
  29. {
  30. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  31. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  32. 95, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
  33. 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
  34. 64,
  35. 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,
  36. 91, 92, 93, 94,
  37. 95, 96,
  38. 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,
  39. 123, 124, 125, 126, 127,
  40. 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
  41. 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
  42. 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
  43. 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
  44. 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
  45. 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
  46. 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
  47. 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255
  48. };
  49. int is_abbrev(char *str1, char *str2)
  50. {
  51. char buf[NUMBER_SIZE], *str3;
  52. str3 = buf;
  53. if (*str1 == 0)
  54. {
  55. return FALSE;
  56. }
  57. if (*str2 == 0)
  58. {
  59. tintin_printf2(gtd->ses, "\e[1;31mis_abbrev(%s,%s)", str1, str2);
  60. dump_stack();
  61. return FALSE;
  62. }
  63. while (TRUE)
  64. {
  65. if (*str1 == 0)
  66. {
  67. *str3 = 0;
  68. strcpy(gtd->is_result, buf);
  69. return TRUE;
  70. }
  71. if (case_table[(int) *str1] != case_table[(int) *str2])
  72. {
  73. return FALSE;
  74. }
  75. str1++;
  76. *str3++ = *str2++;
  77. }
  78. }
  79. int is_member(char *str1, char *str2)
  80. {
  81. char *pt1, *pt2;
  82. if (*str1 == 0)
  83. {
  84. return FALSE;
  85. }
  86. if (*str2 == 0)
  87. {
  88. tintin_printf2(gtd->ses, "\e[1;31mis_member(%s,%s)", str1, str2);
  89. dump_stack();
  90. return FALSE;
  91. }
  92. while (*str1)
  93. {
  94. if (case_table[(int) *str1] == case_table[(int) *str2])
  95. {
  96. pt1 = str1;
  97. pt2 = str2;
  98. while (case_table[(int) *pt1] == case_table[(int) *pt2])
  99. {
  100. pt1++;
  101. pt2++;
  102. if (*pt1 == ' ' || *pt1 == 0)
  103. {
  104. return TRUE;
  105. }
  106. }
  107. }
  108. while (*str1 && *str1 != ' ')
  109. {
  110. str1++;
  111. }
  112. if (*str1)
  113. {
  114. str1++;
  115. }
  116. }
  117. return FALSE;
  118. }
  119. void filename_string(char *input, char *output)
  120. {
  121. while (*input)
  122. {
  123. *output++ = (char) case_table[(int) *input++];
  124. }
  125. *output = 0;
  126. }
  127. int is_vowel(char *str)
  128. {
  129. switch (case_table[(int) *str])
  130. {
  131. case 'a':
  132. case 'e':
  133. case 'i':
  134. case 'o':
  135. case 'u':
  136. return TRUE;
  137. }
  138. return FALSE;
  139. }
  140. struct session *parse_input(struct session *ses, char *input)
  141. {
  142. char *line;
  143. push_call("parse_input(%s,%s)",ses->name,input);
  144. /*
  145. if (*input == 0)
  146. {
  147. write_mud(ses, input, SUB_EOL);
  148. pop_call();
  149. return ses;
  150. }
  151. */
  152. line = str_alloc_stack(0);
  153. if (VERBATIM(ses))
  154. {
  155. sub_arg_all(ses, input, line, 1, SUB_SEC);
  156. if (check_all_aliases(ses, line))
  157. {
  158. ses = script_driver(ses, LIST_ALIAS, line);
  159. }
  160. else if (HAS_BIT(ses->config_flags, CONFIG_FLAG_SPEEDWALK) && is_speedwalk(ses, line))
  161. {
  162. process_speedwalk(ses, line);
  163. }
  164. else
  165. {
  166. write_mud(ses, line, SUB_EOL|SUB_ESC);
  167. }
  168. pop_call();
  169. return ses;
  170. }
  171. if (*input == gtd->verbatim_char)
  172. {
  173. write_mud(ses, input+1, SUB_EOL);
  174. pop_call();
  175. return ses;
  176. }
  177. do
  178. {
  179. input = space_out(input);
  180. input = get_arg_all(ses, input, line, 0);
  181. if (parse_command(ses, line))
  182. {
  183. ses = script_driver(ses, LIST_COMMAND, line);
  184. }
  185. else if (check_all_aliases(ses, line))
  186. {
  187. ses = script_driver(ses, LIST_ALIAS, line);
  188. }
  189. else if (HAS_BIT(ses->config_flags, CONFIG_FLAG_SPEEDWALK) && is_speedwalk(ses, line))
  190. {
  191. process_speedwalk(ses, line);
  192. }
  193. else
  194. {
  195. write_mud(ses, line, SUB_VAR|SUB_FUN|SUB_ESC|SUB_EOL);
  196. }
  197. if (*input == COMMAND_SEPARATOR)
  198. {
  199. input++;
  200. }
  201. }
  202. while (*input);
  203. pop_call();
  204. return ses;
  205. }
  206. /*
  207. Deal with variables and functions used as commands.
  208. */
  209. struct session *parse_command(struct session *ses, char *input)
  210. {
  211. char *arg, *arg1;
  212. push_call("parse_command(%p,%p)",ses,input);
  213. arg1 = str_alloc_stack(0);
  214. arg = sub_arg_stop_spaces(ses, input, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  215. if (!strncmp(input, arg1, strlen(arg1)))
  216. {
  217. pop_call();
  218. return NULL;
  219. }
  220. if (*arg)
  221. {
  222. cat_sprintf(arg1, " %s", arg);
  223. }
  224. strcpy(input, arg1);
  225. pop_call();
  226. return ses;
  227. }
  228. char *substitute_speedwalk(struct session *ses, char *input, char *output)
  229. {
  230. char num[NUMBER_SIZE], name[BUFFER_SIZE], *pti, *ptn, *pto;
  231. int cnt, max;
  232. pti = input;
  233. pto = output;
  234. while (*pti && pto - output < INPUT_SIZE)
  235. {
  236. if (is_space(*pti))
  237. {
  238. return input;
  239. }
  240. if (is_digit(*pti))
  241. {
  242. ptn = num;
  243. while (is_digit(*pti))
  244. {
  245. if (ptn - num < 4)
  246. {
  247. *ptn++ = *pti++;
  248. }
  249. else
  250. {
  251. pti++;
  252. }
  253. }
  254. *ptn = 0;
  255. max = atoi(num);
  256. if (*pti == 0)
  257. {
  258. return input;
  259. }
  260. }
  261. else
  262. {
  263. max = 1;
  264. }
  265. pti = get_arg_stop_digits(ses, pti, name, GET_ONE);
  266. if (*name == 0 || !is_pathdir(ses, name))
  267. {
  268. return input;
  269. }
  270. for (cnt = 0 ; cnt < max ; cnt++)
  271. {
  272. if (output != pto)
  273. {
  274. *pto++ = COMMAND_SEPARATOR;
  275. }
  276. pto += sprintf(pto, "%s", name);
  277. }
  278. if (*pti == COMMAND_SEPARATOR)
  279. {
  280. pti++;
  281. }
  282. }
  283. *pto = 0;
  284. return output;
  285. }
  286. int is_speedwalk(struct session *ses, char *input)
  287. {
  288. int digit = 0, flag = FALSE;
  289. while (*input)
  290. {
  291. switch (*input)
  292. {
  293. case 'n':
  294. case 'e':
  295. case 's':
  296. case 'w':
  297. case 'u':
  298. case 'd':
  299. if (digit > 3)
  300. {
  301. return FALSE;
  302. }
  303. digit = 0;
  304. flag = TRUE;
  305. break;
  306. case '0':
  307. case '1':
  308. case '2':
  309. case '3':
  310. case '4':
  311. case '5':
  312. case '6':
  313. case '7':
  314. case '8':
  315. case '9':
  316. digit++;
  317. flag = FALSE;
  318. break;
  319. default:
  320. return FALSE;
  321. }
  322. input++;
  323. }
  324. return flag;
  325. }
  326. void process_speedwalk(struct session *ses, char *input)
  327. {
  328. char dir[2];
  329. int cnt, i;
  330. for (dir[1] = 0 ; *input ; input++)
  331. {
  332. if (is_digit(*input))
  333. {
  334. sscanf(input, "%d%c", &cnt, dir);
  335. for (i = 0 ; i < cnt ; i++)
  336. {
  337. write_mud(ses, dir, SUB_EOL);
  338. }
  339. while (*input != dir[0])
  340. {
  341. input++;
  342. }
  343. }
  344. else
  345. {
  346. dir[0] = *input;
  347. write_mud(ses, dir, SUB_EOL);
  348. }
  349. }
  350. return;
  351. }
  352. /*
  353. Deals with all # stuff
  354. */
  355. struct session *parse_tintin_command(struct session *ses, char *input)
  356. {
  357. char line[BUFFER_SIZE];
  358. struct session *sesptr;
  359. input = sub_arg_in_braces(ses, input, line, GET_ONE, SUB_VAR|SUB_FUN);
  360. if (is_number(line))
  361. {
  362. int cnt = atoi(line);
  363. input = get_arg_in_braces(ses, input, line, GET_ALL);
  364. while (cnt-- > 0)
  365. {
  366. ses = script_driver(ses, LIST_COMMAND, line);
  367. }
  368. return ses;
  369. }
  370. sesptr = find_session(line);
  371. if (sesptr)
  372. {
  373. if (*input)
  374. {
  375. input = get_arg_in_braces(ses, input, line, GET_ALL);
  376. substitute(ses, line, line, SUB_VAR|SUB_FUN);
  377. script_driver(sesptr, LIST_COMMAND, line);
  378. return ses;
  379. }
  380. else
  381. {
  382. return activate_session(sesptr);
  383. }
  384. }
  385. if (*line == '!')
  386. {
  387. show_error(ses, LIST_COMMAND, "#!%s %s", line + 1, input);
  388. return ses;
  389. }
  390. tintin_printf2(ses, "#ERROR: #UNKNOWN TINTIN-COMMAND '%s'.", line);
  391. check_all_events(ses, SUB_SEC|EVENT_FLAG_SYSTEM, 0, 1, "UNKNOWN COMMAND", line);
  392. return ses;
  393. }
  394. int cnt_arg_all(struct session *ses, char *string, int flag)
  395. {
  396. char *arg, tmp[BUFFER_SIZE];
  397. int cnt;
  398. arg = string;
  399. cnt = 0;
  400. while (*arg)
  401. {
  402. cnt++;
  403. arg = get_arg_in_braces(ses, arg, tmp, flag);
  404. if (*arg == COMMAND_SEPARATOR)
  405. {
  406. arg++;
  407. }
  408. }
  409. return cnt;
  410. }
  411. /*
  412. get all arguments - only check for unescaped command separators
  413. */
  414. char *get_arg_all(struct session *ses, char *string, char *result, int verbatim)
  415. {
  416. char *pto, *pti;
  417. int skip, nest = 0;
  418. pti = string;
  419. pto = result;
  420. if (*pti == gtd->verbatim_char)
  421. {
  422. while (*pti)
  423. {
  424. *pto++ = *pti++;
  425. }
  426. *pto = 0;
  427. return pti;
  428. }
  429. while (*pti)
  430. {
  431. if (HAS_BIT(ses->charset, CHARSET_FLAG_EUC) && is_euc_head(ses, pti))
  432. {
  433. *pto++ = *pti++;
  434. *pto++ = *pti++;
  435. continue;
  436. }
  437. skip = find_secure_color_code(pti);
  438. if (skip)
  439. {
  440. while (skip--)
  441. {
  442. *pto++ = *pti++;
  443. }
  444. continue;
  445. }
  446. if (*pti == '\\' && pti[1] == COMMAND_SEPARATOR)
  447. {
  448. *pto++ = *pti++;
  449. }
  450. else if (*pti == COMMAND_SEPARATOR && nest == 0 && !verbatim)
  451. {
  452. break;
  453. }
  454. else if (*pti == DEFAULT_OPEN)
  455. {
  456. nest++;
  457. }
  458. else if (*pti == DEFAULT_CLOSE)
  459. {
  460. nest--;
  461. }
  462. *pto++ = *pti++;
  463. if (pto - result >= BUFFER_SIZE - 3)
  464. {
  465. tintin_printf2(ses, "#ERROR: INPUT BUFFER OVERFLOW.");
  466. pto--;
  467. break;
  468. }
  469. }
  470. *pto = '\0';
  471. return pti;
  472. }
  473. char *sub_arg_all(struct session *ses, char *string, char *result, int verbatim, int sub)
  474. {
  475. char *buffer;
  476. if (*string == 0)
  477. {
  478. *result = 0;
  479. return string;
  480. }
  481. push_call("sub_arg_all(%p,%p,%p,%d,%d)",ses,string,result,verbatim,sub);
  482. buffer = str_alloc_stack(strlen(string));
  483. string = get_arg_all(ses, string, buffer, verbatim);
  484. substitute(ses, buffer, result, sub);
  485. pop_call();
  486. return string;
  487. }
  488. /*
  489. Braces are stripped in braced arguments leaving all else as is.
  490. */
  491. char *get_arg_in_braces(struct session *ses, char *string, char *result, int flag)
  492. {
  493. char *pti, *pto;
  494. int skip, nest = 1;
  495. pti = HAS_BIT(flag, GET_SPC) ? string : space_out(string);
  496. pto = result;
  497. if (*pti != DEFAULT_OPEN)
  498. {
  499. if (!HAS_BIT(flag, GET_ALL))
  500. {
  501. pti = get_arg_stop_spaces(ses, pti, result, flag);
  502. }
  503. else
  504. {
  505. pti = get_arg_with_spaces(ses, pti, result, flag);
  506. }
  507. return pti;
  508. }
  509. pti++;
  510. while (*pti)
  511. {
  512. if (HAS_BIT(ses->charset, CHARSET_FLAG_EUC) && is_euc_head(ses, pti))
  513. {
  514. *pto++ = *pti++;
  515. *pto++ = *pti++;
  516. continue;
  517. }
  518. skip = find_secure_color_code(pti);
  519. if (skip)
  520. {
  521. while (skip--)
  522. {
  523. *pto++ = *pti++;
  524. }
  525. continue;
  526. }
  527. if (*pti == DEFAULT_OPEN)
  528. {
  529. nest++;
  530. }
  531. else if (*pti == DEFAULT_CLOSE)
  532. {
  533. nest--;
  534. if (nest == 0)
  535. {
  536. break;
  537. }
  538. }
  539. *pto++ = *pti++;
  540. }
  541. if (*pti == 0)
  542. {
  543. show_error(ses, LIST_COMMAND, "#ERROR: GET BRACED ARGUMENT: UNMATCHED BRACE.");
  544. }
  545. else
  546. {
  547. pti++;
  548. }
  549. *pto = '\0';
  550. return pti;
  551. }
  552. char *sub_arg_in_braces(struct session *ses, char *string, char *result, int flag, int sub)
  553. {
  554. char *buffer;
  555. if (*string == 0)
  556. {
  557. *result = 0;
  558. return string;
  559. }
  560. push_call("sub_arg_in_braces(%p,%p,%p,%d,%d)",ses,string,result,flag,sub);
  561. buffer = str_alloc_stack(strlen(string) * 2);
  562. string = get_arg_in_braces(ses, string, buffer, flag);
  563. substitute(ses, buffer, result, sub);
  564. pop_call();
  565. return string;
  566. }
  567. /*
  568. get all arguments
  569. */
  570. char *get_arg_with_spaces(struct session *ses, char *string, char *result, int flag)
  571. {
  572. char *pto, *pti;
  573. int skip, nest = 0;
  574. pti = HAS_BIT(flag, GET_SPC) ? string : space_out(string);
  575. pto = result;
  576. while (*pti)
  577. {
  578. if (HAS_BIT(ses->charset, CHARSET_FLAG_EUC) && is_euc_head(ses, pti))
  579. {
  580. *pto++ = *pti++;
  581. *pto++ = *pti++;
  582. continue;
  583. }
  584. skip = find_secure_color_code(pti);
  585. if (skip)
  586. {
  587. while (skip--)
  588. {
  589. *pto++ = *pti++;
  590. }
  591. continue;
  592. }
  593. if (*pti == '\\' && pti[1] == COMMAND_SEPARATOR)
  594. {
  595. *pto++ = *pti++;
  596. }
  597. else if (*pti == COMMAND_SEPARATOR && nest == 0)
  598. {
  599. break;
  600. }
  601. else if (*pti == DEFAULT_OPEN)
  602. {
  603. nest++;
  604. }
  605. else if (*pti == DEFAULT_CLOSE)
  606. {
  607. nest--;
  608. }
  609. *pto++ = *pti++;
  610. }
  611. *pto = '\0';
  612. return pti;
  613. }
  614. /*
  615. get one arg, stop at spaces
  616. */
  617. char *get_arg_stop_spaces(struct session *ses, char *string, char *result, int flag)
  618. {
  619. char *pto, *pti;
  620. int skip, nest = 0;
  621. pti = space_out(string);
  622. pto = result;
  623. while (*pti)
  624. {
  625. if (HAS_BIT(ses->charset, CHARSET_FLAG_EUC) && is_euc_head(ses, pti))
  626. {
  627. *pto++ = *pti++;
  628. *pto++ = *pti++;
  629. continue;
  630. }
  631. skip = find_secure_color_code(pti);
  632. if (skip)
  633. {
  634. while (skip--)
  635. {
  636. *pto++ = *pti++;
  637. }
  638. continue;
  639. }
  640. if (*pti == '\\' && pti[1] == COMMAND_SEPARATOR)
  641. {
  642. *pto++ = *pti++;
  643. }
  644. else if (*pti == COMMAND_SEPARATOR && nest == 0)
  645. {
  646. break;
  647. }
  648. else if (is_space(*pti) && nest == 0)
  649. {
  650. pti++;
  651. break;
  652. }
  653. else if (*pti == DEFAULT_OPEN)
  654. {
  655. nest++;
  656. }
  657. else if (*pti == '[' && HAS_BIT(flag, GET_NST))
  658. {
  659. nest++;
  660. }
  661. else if (*pti == DEFAULT_CLOSE)
  662. {
  663. nest--;
  664. }
  665. else if (*pti == ']' && HAS_BIT(flag, GET_NST))
  666. {
  667. nest--;
  668. }
  669. *pto++ = *pti++;
  670. }
  671. *pto = '\0';
  672. return pti;
  673. }
  674. char *sub_arg_stop_spaces(struct session *ses, char *string, char *result, int flag, int sub)
  675. {
  676. char *buffer;
  677. if (*string == 0)
  678. {
  679. *result = 0;
  680. return string;
  681. }
  682. push_call("sub_arg_stop_braces(%p,%p,%p,%d,%d)",ses,string,result,flag,sub);
  683. buffer = str_alloc_stack(strlen(string) * 2);
  684. string = get_arg_stop_spaces(ses, string, buffer, flag);
  685. substitute(ses, buffer, result, sub);
  686. pop_call();
  687. return string;
  688. }
  689. // Get one arg, stop at numbers, used for speedwalks
  690. char *get_arg_stop_digits(struct session *ses, char *string, char *result, int flag)
  691. {
  692. char *pto, *pti;
  693. int nest = 0;
  694. pti = space_out(string);
  695. pto = result;
  696. while (*pti)
  697. {
  698. if (HAS_BIT(ses->charset, CHARSET_FLAG_EUC) && is_euc_head(ses, pti))
  699. {
  700. *pto++ = *pti++;
  701. *pto++ = *pti++;
  702. continue;
  703. }
  704. if (*pti == '\\' && pti[1] == COMMAND_SEPARATOR)
  705. {
  706. *pto++ = *pti++;
  707. }
  708. else if (*pti == COMMAND_SEPARATOR && nest == 0)
  709. {
  710. break;
  711. }
  712. else if (is_digit(*pti) && nest == 0)
  713. {
  714. break;
  715. }
  716. else if (*pti == DEFAULT_OPEN)
  717. {
  718. nest++;
  719. }
  720. else if (*pti == '[' && HAS_BIT(flag, GET_NST))
  721. {
  722. nest++;
  723. }
  724. else if (*pti == DEFAULT_CLOSE)
  725. {
  726. nest--;
  727. }
  728. else if (*pti == ']' && HAS_BIT(flag, GET_NST))
  729. {
  730. nest--;
  731. }
  732. *pto++ = *pti++;
  733. }
  734. *pto = '\0';
  735. return pti;
  736. }
  737. /*
  738. advance ptr to next none-space
  739. */
  740. char *space_out(char *string)
  741. {
  742. while (is_space(*string))
  743. {
  744. string++;
  745. }
  746. return string;
  747. }
  748. /*
  749. For variable handling
  750. */
  751. char *get_arg_to_brackets(struct session *ses, char *string, char *result)
  752. {
  753. char *pti, *pto, *ptii, *ptoo;
  754. int nest1 = 0, nest2 = 0, nest3 = 0;
  755. pti = space_out(string);
  756. pto = result;
  757. ptii = ptoo = NULL;
  758. while (*pti)
  759. {
  760. if (HAS_BIT(ses->charset, CHARSET_FLAG_EUC) && is_euc_head(ses, pti))
  761. {
  762. *pto++ = *pti++;
  763. *pto++ = *pti++;
  764. continue;
  765. }
  766. if (*pti == '[')
  767. {
  768. nest2++;
  769. if (nest1 == 0 && ptii == NULL)
  770. {
  771. ptii = pti;
  772. ptoo = pto;
  773. }
  774. }
  775. else if (*pti == ']')
  776. {
  777. if (nest2)
  778. {
  779. nest2--;
  780. }
  781. else
  782. {
  783. nest3 = 1;
  784. }
  785. if (*(pti+1) == 0 && ptii && nest1 == 0 && nest2 == 0 && nest3 == 0)
  786. {
  787. *ptoo = 0;
  788. return ptii;
  789. }
  790. }
  791. else if (*pti == DEFAULT_OPEN)
  792. {
  793. nest1++;
  794. }
  795. else if (*pti == DEFAULT_CLOSE)
  796. {
  797. nest1--;
  798. }
  799. *pto++ = *pti++;
  800. }
  801. *pto = 0;
  802. return pti;
  803. }
  804. char *get_arg_at_brackets(struct session *ses, char *string, char *result)
  805. {
  806. char *pti, *pto;
  807. int nest = 0;
  808. pti = string;
  809. pto = result;
  810. if (*pti != '[')
  811. {
  812. *pto = 0;
  813. return pti;
  814. }
  815. while (*pti)
  816. {
  817. if (HAS_BIT(ses->charset, CHARSET_FLAG_EUC) && is_euc_head(ses, pti))
  818. {
  819. *pto++ = *pti++;
  820. *pto++ = *pti++;
  821. continue;
  822. }
  823. if (*pti == '[')
  824. {
  825. nest++;
  826. }
  827. else if (*pti == ']')
  828. {
  829. if (nest)
  830. {
  831. nest--;
  832. }
  833. else
  834. {
  835. break;
  836. }
  837. }
  838. else if (nest == 0)
  839. {
  840. break;
  841. }
  842. *pto++ = *pti++;
  843. }
  844. if (nest)
  845. {
  846. show_error(ses, LIST_COMMAND, "#ERROR: GET BRACKETED VARIABLE: UNMATCHED BRACKET.");
  847. }
  848. *pto = 0;
  849. return pti;
  850. }
  851. char *get_arg_in_brackets(struct session *ses, char *string, char *result)
  852. {
  853. char *pti, *pto;
  854. int nest = 1;
  855. pti = string;
  856. pto = result;
  857. if (*pti != '[')
  858. {
  859. *pto = 0;
  860. return pti;
  861. }
  862. pti++;
  863. while (*pti)
  864. {
  865. if (HAS_BIT(ses->charset, CHARSET_FLAG_EUC) && is_euc_head(ses, pti))
  866. {
  867. *pto++ = *pti++;
  868. *pto++ = *pti++;
  869. continue;
  870. }
  871. if (*pti == '[')
  872. {
  873. nest++;
  874. }
  875. else if (*pti == ']')
  876. {
  877. nest--;
  878. if (nest == 0)
  879. {
  880. break;
  881. }
  882. }
  883. *pto++ = *pti++;
  884. }
  885. if (*pti == 0)
  886. {
  887. show_error(ses, LIST_COMMAND, "#ERROR: GET BRACKETED ARGUMENT: UNMATCHED BRACKET.");
  888. }
  889. else
  890. {
  891. pti++;
  892. }
  893. *pto = 0;
  894. return pti;
  895. }
  896. char *get_char(struct session *ses, char *string, char *result)
  897. {
  898. char *pti = string;
  899. if (HAS_BIT(ses->charset, CHARSET_FLAG_EUC) && is_euc_head(ses, pti))
  900. {
  901. pti += sprintf(result, "%c%c", pti[0], pti[1]);
  902. }
  903. else if (HAS_BIT(ses->charset, CHARSET_FLAG_UTF8) && is_utf8_head(pti))
  904. {
  905. pti += snprintf(result, 6, "%.*s", get_utf8_size(pti), pti);
  906. }
  907. else
  908. {
  909. pti += sprintf(result, "%c", pti[0]);
  910. }
  911. return pti;
  912. }
  913. /*
  914. send command to the mud
  915. */
  916. void write_mud(struct session *ses, char *command, int flags)
  917. {
  918. char output[BUFFER_SIZE];
  919. int size;
  920. size = substitute(ses, command, output, flags);
  921. if (gtd->level->ignore == 0 && HAS_BIT(ses->flags, SES_FLAG_PATHMAPPING))
  922. {
  923. if (ses->map == NULL || ses->map->nofollow == 0)
  924. {
  925. check_append_path(ses, command, NULL, 0.0, 0, 1);
  926. }
  927. }
  928. if (gtd->level->ignore == 0 && ses->map && ses->map->in_room && ses->map->nofollow == 0)
  929. {
  930. int quiet, follow;
  931. quiet = HAS_BIT(ses->map->flags, MAP_FLAG_QUIET);
  932. gtd->level->input += quiet;
  933. follow = follow_map(ses, command);
  934. gtd->level->input -= quiet;
  935. if (follow)
  936. {
  937. return;
  938. }
  939. }
  940. write_line_mud(ses, output, size);
  941. }
  942. /*
  943. Check line for triggers
  944. */
  945. void do_one_line(char *line, struct session *ses)
  946. {
  947. char *strip, *buf;
  948. if (gtd->level->ignore)
  949. {
  950. return;
  951. }
  952. push_call("do_one_line(%s,%p)",ses->name,line);
  953. push_script_stack(ses, LIST_VARIABLE);
  954. strip = str_alloc_stack(0);
  955. buf = str_alloc_stack(0);
  956. strip_vt102_codes(line, strip);
  957. if (!HAS_BIT(ses->list[LIST_ACTION]->flags, LIST_FLAG_IGNORE))
  958. {
  959. check_all_actions(ses, line, strip, buf);
  960. }
  961. if (!HAS_BIT(ses->list[LIST_PROMPT]->flags, LIST_FLAG_IGNORE))
  962. {
  963. check_all_prompts(ses, line, strip);
  964. }
  965. if (!HAS_BIT(ses->list[LIST_GAG]->flags, LIST_FLAG_IGNORE))
  966. {
  967. check_all_gags(ses, line, strip);
  968. }
  969. if (!HAS_BIT(ses->list[LIST_SUBSTITUTE]->flags, LIST_FLAG_IGNORE))
  970. {
  971. check_all_substitutions(ses, line, strip);
  972. }
  973. if (!HAS_BIT(ses->list[LIST_HIGHLIGHT]->flags, LIST_FLAG_IGNORE))
  974. {
  975. check_all_highlights(ses, line, strip);
  976. }
  977. if (HAS_BIT(ses->log->mode, LOG_FLAG_NEXT))
  978. {
  979. logit(ses, line, ses->log->next_file, LOG_FLAG_LINEFEED);
  980. DEL_BIT(ses->log->mode, LOG_FLAG_NEXT);
  981. }
  982. pop_script_stack();
  983. pop_call();
  984. return;
  985. }