parse.c 20 KB

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