parse.c 21 KB

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