line.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. /******************************************************************************
  2. * This file is part of TinTin++ *
  3. * *
  4. * Copyright 2004-2020 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. * You should have received a copy of the GNU General Public License *
  17. * along with TinTin++. If not, see https://www.gnu.org/licenses. *
  18. ******************************************************************************/
  19. /******************************************************************************
  20. * T I N T I N + + *
  21. * *
  22. * coded by Igor van den Hoven 2008 *
  23. ******************************************************************************/
  24. #include "tintin.h"
  25. struct line_type
  26. {
  27. char * name;
  28. LINE * fun;
  29. char * desc;
  30. };
  31. struct line_type line_table[] =
  32. {
  33. { "BACKGROUND", line_background, "Execute line without stealing session focus." },
  34. { "BENCHMARK", line_benchmark, "Execute line and provide timing information." },
  35. { "CAPTURE", line_capture, "Capture output in the given variable." },
  36. { "CONVERT", line_convert, "Execute line in convert meta data mode." },
  37. { "DEBUG", line_debug, "Execute line in debug mode." },
  38. { "GAG", line_gag, "Gag the next line." },
  39. { "IGNORE", line_ignore, "Execute line with triggers ignored." },
  40. { "JSON", line_json, "Execute line with json conversion." },
  41. { "LOCAL", line_local, "Execute line with local scope." },
  42. { "LOG", line_log, "Log the next line or given line." },
  43. { "LOGMODE", line_logmode, "Execute line with given log mode." },
  44. { "LOGVERBATIM", line_logverbatim, "Log the line as plain text verbatim." },
  45. { "MSDP", line_msdp, "Execute line with msdp conversion." },
  46. { "MULTISHOT", line_multishot, "Execute line creating multishot triggers." },
  47. { "ONESHOT", line_oneshot, "Execute line creating oneshot triggers." },
  48. { "QUIET", line_quiet, "Execute line with all system messages off." },
  49. { "STRIP", line_strip, "Execute line with escape codes stripped." },
  50. { "SUBSTITUTE", line_substitute, "Execute line with given substitution." },
  51. { "VERBATIM", line_verbatim, "Execute line as plain text." },
  52. { "VERBOSE", line_verbose, "Execute line with all system messages on." },
  53. { "", NULL, "" }
  54. };
  55. DO_COMMAND(do_line)
  56. {
  57. int cnt;
  58. arg = get_arg_in_braces(ses, arg, arg1, GET_ONE);
  59. if (*arg1 == 0)
  60. {
  61. info:
  62. tintin_header(ses, 80, " LINE OPTIONS ");
  63. for (cnt = 0 ; *line_table[cnt].fun != NULL ; cnt++)
  64. {
  65. if (*line_table[cnt].desc)
  66. {
  67. tintin_printf2(ses, " [%-13s] %s", line_table[cnt].name, line_table[cnt].desc);
  68. }
  69. }
  70. tintin_header(ses, 80, "");
  71. return ses;
  72. }
  73. else
  74. {
  75. for (cnt = 0 ; *line_table[cnt].name ; cnt++)
  76. {
  77. if (is_abbrev(arg1, line_table[cnt].name))
  78. {
  79. break;
  80. }
  81. }
  82. if (*line_table[cnt].name == 0)
  83. {
  84. goto info;
  85. }
  86. else
  87. {
  88. ses = line_table[cnt].fun(ses, arg, arg1, arg2, arg3);
  89. }
  90. }
  91. return ses;
  92. }
  93. DO_LINE(line_background)
  94. {
  95. arg = get_arg_in_braces(ses, arg, arg1, GET_ALL);
  96. if (*arg1 == 0)
  97. {
  98. show_error(ses, LIST_COMMAND, "#SYNTAX: #LINE {BACKGROUND} {command}.");
  99. return ses;
  100. }
  101. gtd->level->background++;
  102. ses = script_driver(ses, LIST_COMMAND, arg1);
  103. gtd->level->background--;
  104. return ses;
  105. }
  106. DO_LINE(line_benchmark)
  107. {
  108. long long start, end;
  109. arg = get_arg_in_braces(ses, arg, arg1, GET_ALL);
  110. if (*arg1 == 0)
  111. {
  112. show_error(ses, LIST_COMMAND, "#SYNTAX: #LINE {BENCHMARK} {command}.");
  113. return ses;
  114. }
  115. start = utime();
  116. ses = script_driver(ses, LIST_COMMAND, arg1);
  117. end = utime();
  118. tintin_printf2(ses, "#LINE BENCHMARK: %lld USEC.", end - start);
  119. return ses;
  120. }
  121. DO_LINE(line_capture)
  122. {
  123. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  124. arg = sub_arg_in_braces(ses, arg, arg2, GET_ALL, SUB_VAR|SUB_FUN);
  125. if (*arg1 && *arg2)
  126. {
  127. if (ses->line_capturefile)
  128. {
  129. free(ses->line_capturefile);
  130. }
  131. ses->line_capturefile = strdup(arg1);
  132. ses->line_captureindex = 1;
  133. ses = script_driver(ses, LIST_COMMAND, arg2);
  134. if (ses->line_capturefile)
  135. {
  136. free(ses->line_capturefile);
  137. ses->line_capturefile = NULL;
  138. }
  139. }
  140. else
  141. {
  142. show_error(ses, LIST_COMMAND, "#SYNTAX: #LINE CAPTURE {VARIABLE} {command}.");
  143. }
  144. return ses;
  145. }
  146. DO_LINE(line_convert)
  147. {
  148. arg = get_arg_in_braces(ses, arg, arg1, GET_ALL);
  149. if (*arg1 == 0)
  150. {
  151. show_error(ses, LIST_COMMAND, "#SYNTAX: #LINE {CONVERT} {command}.");
  152. return ses;
  153. }
  154. // May need a clearer flag here.
  155. gtd->level->convert++;
  156. ses = script_driver(ses, LIST_COMMAND, arg1);
  157. gtd->level->convert--;
  158. return ses;
  159. }
  160. DO_LINE(line_debug)
  161. {
  162. arg = get_arg_in_braces(ses, arg, arg1, GET_ALL);
  163. if (*arg1 == 0)
  164. {
  165. show_error(ses, LIST_COMMAND, "#SYNTAX: #LINE {DEBUG} {command}.");
  166. return ses;
  167. }
  168. gtd->level->debug++;
  169. ses = script_driver(ses, LIST_COMMAND, arg1);
  170. gtd->level->debug--;
  171. return ses;
  172. }
  173. DO_LINE(line_gag)
  174. {
  175. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  176. switch (*arg1)
  177. {
  178. case '-':
  179. ses->gagline -= get_number(ses, arg1+1);
  180. break;
  181. case '+':
  182. ses->gagline += get_number(ses, arg1+1);
  183. break;
  184. case 0:
  185. ses->gagline = 1;
  186. break;
  187. default:
  188. ses->gagline = get_number(ses, arg1);
  189. break;
  190. }
  191. if (ses->gagline < 0)
  192. {
  193. ses->gagline = 0;
  194. }
  195. show_debug(ses, LIST_GAG, COLOR_DEBUG "#DEBUG LINE GAG " COLOR_BRACE "{" COLOR_STRING "%s" COLOR_BRACE "} " COLOR_COMMAND "[" COLOR_STRING "%d" COLOR_COMMAND "]", arg1, ses->gagline);
  196. return ses;
  197. }
  198. DO_LINE(line_ignore)
  199. {
  200. arg = get_arg_in_braces(ses, arg, arg1, GET_ALL);
  201. if (*arg1 == 0)
  202. {
  203. show_error(ses, LIST_COMMAND, "#SYNTAX: #LINE {IGNORE} {command}.");
  204. return ses;
  205. }
  206. gtd->level->ignore++;
  207. ses = script_driver(ses, LIST_COMMAND, arg1);
  208. gtd->level->ignore--;
  209. return ses;
  210. }
  211. DO_LINE(line_local)
  212. {
  213. arg = get_arg_in_braces(ses, arg, arg1, GET_ALL);
  214. if (*arg1 == 0)
  215. {
  216. show_error(ses, LIST_COMMAND, "#SYNTAX: #LINE {LOCAL} {command}.");
  217. return ses;
  218. }
  219. gtd->level->local++;
  220. SET_BIT(gtd->flags, TINTIN_FLAG_LOCAL);
  221. ses = script_driver(ses, LIST_COMMAND, arg1);
  222. gtd->level->local--;
  223. return ses;
  224. }
  225. // Without an argument mark next line to be logged, otherwise log the given line to file.
  226. DO_LINE(line_log)
  227. {
  228. FILE *logfile;
  229. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  230. arg = sub_arg_in_braces(ses, arg, arg2, GET_ALL, SUB_VAR|SUB_FUN);
  231. if (*arg1 && *arg2)
  232. {
  233. substitute(ses, arg2, arg2, SUB_ESC|SUB_COL|SUB_LNF);
  234. if (ses->log->file && !strcmp(ses->log->name, arg1))
  235. {
  236. logit(ses, arg2, ses->log->file, LOG_FLAG_NONE);
  237. }
  238. else if (ses->log->line_time == gtd->time && !strcmp(ses->log->line_name, arg1))
  239. {
  240. logit(ses, arg2, ses->log->line_file, LOG_FLAG_NONE);
  241. }
  242. else
  243. {
  244. if ((logfile = fopen(arg1, "a")))
  245. {
  246. if (ses->log->line_file)
  247. {
  248. fclose(ses->log->line_file);
  249. }
  250. free(ses->log->line_name);
  251. ses->log->line_name = strdup(arg1);
  252. ses->log->line_file = logfile;
  253. ses->log->line_time = gtd->time;
  254. logheader(ses, ses->log->line_file, LOG_FLAG_APPEND | HAS_BIT(ses->log->mode, LOG_FLAG_HTML));
  255. logit(ses, arg2, ses->log->line_file, LOG_FLAG_NONE);
  256. }
  257. else
  258. {
  259. show_error(ses, LIST_COMMAND, "#ERROR: #LINE LOG {%s} - COULDN'T OPEN FILE.", arg1);
  260. }
  261. }
  262. }
  263. else if (*arg1)
  264. {
  265. if (ses->log->next_time == gtd->time && !strcmp(ses->log->next_name, arg1))
  266. {
  267. SET_BIT(ses->log->mode, LOG_FLAG_NEXT);
  268. }
  269. else if ((logfile = fopen(arg1, "a")))
  270. {
  271. if (ses->log->next_file)
  272. {
  273. fclose(ses->log->next_file);
  274. }
  275. free(ses->log->next_name);
  276. ses->log->next_name = strdup(arg1);
  277. ses->log->next_file = logfile;
  278. ses->log->next_time = gtd->time;
  279. SET_BIT(ses->log->mode, LOG_FLAG_NEXT);
  280. }
  281. else
  282. {
  283. show_error(ses, LIST_COMMAND, "#ERROR: #LINE LOG {%s} - COULDN'T OPEN FILE.", arg1);
  284. }
  285. }
  286. else
  287. {
  288. show_error(ses, LIST_COMMAND, "#SYNTAX: #LINE {LOG} {filename} [string]");
  289. }
  290. return ses;
  291. }
  292. DO_LINE(line_logverbatim)
  293. {
  294. FILE *logfile;
  295. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  296. arg = get_arg_in_braces(ses, arg, arg2, GET_ALL);
  297. if (*arg1 && *arg2)
  298. {
  299. if (ses->log->file && !strcmp(ses->log->name, arg1))
  300. {
  301. logit(ses, arg2, ses->log->file, LOG_FLAG_LINEFEED);
  302. }
  303. else if (ses->log->line_time == gtd->time && !strcmp(ses->log->line_name, arg1))
  304. {
  305. logit(ses, arg2, ses->log->line_file, LOG_FLAG_LINEFEED|LOG_FLAG_PLAIN);
  306. }
  307. else
  308. {
  309. if ((logfile = fopen(arg1, "a")))
  310. {
  311. if (ses->log->line_file)
  312. {
  313. fclose(ses->log->line_file);
  314. }
  315. free(ses->log->line_name);
  316. ses->log->line_name = strdup(arg1);
  317. ses->log->line_file = logfile;
  318. ses->log->line_time = gtd->time;
  319. logit(ses, arg2, ses->log->line_file, LOG_FLAG_LINEFEED|LOG_FLAG_PLAIN);
  320. }
  321. else
  322. {
  323. show_error(ses, LIST_COMMAND, "#ERROR: #LINE LOGVERBATIM {%s} - COULDN'T OPEN FILE.", arg1);
  324. }
  325. }
  326. }
  327. else if (*arg1)
  328. {
  329. if (ses->log->next_time == gtd->time && !strcmp(ses->log->next_name, arg1))
  330. {
  331. SET_BIT(ses->log->mode, LOG_FLAG_NEXT);
  332. }
  333. else if ((logfile = fopen(arg1, "a")))
  334. {
  335. if (ses->log->next_file)
  336. {
  337. fclose(ses->log->next_file);
  338. }
  339. free(ses->log->next_name);
  340. ses->log->next_name = strdup(arg1);
  341. ses->log->next_file = logfile;
  342. ses->log->next_time = gtd->time;
  343. SET_BIT(ses->log->mode, LOG_FLAG_NEXT);
  344. }
  345. else
  346. {
  347. show_error(ses, LIST_COMMAND, "#ERROR: #LINE LOGVERBATIM {%s} - COULDN'T OPEN FILE.", arg1);
  348. }
  349. }
  350. else
  351. {
  352. show_error(ses, LIST_COMMAND, "#SYNTAX: #LINE {LOGVERBATIM} {filename} {string}");
  353. }
  354. return ses;
  355. }
  356. DO_LINE(line_logmode)
  357. {
  358. struct session *active_ses;
  359. int old_mode = ses->log->mode;
  360. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  361. if (is_abbrev(arg1, "HTML"))
  362. {
  363. SET_BIT(ses->log->mode, LOG_FLAG_HTML);
  364. DEL_BIT(ses->log->mode, LOG_FLAG_PLAIN);
  365. DEL_BIT(ses->log->mode, LOG_FLAG_RAW);
  366. }
  367. else if (is_abbrev(arg1, "PLAIN"))
  368. {
  369. SET_BIT(ses->log->mode, LOG_FLAG_PLAIN);
  370. DEL_BIT(ses->log->mode, LOG_FLAG_HTML);
  371. DEL_BIT(ses->log->mode, LOG_FLAG_RAW);
  372. }
  373. else if (is_abbrev(arg1, "RAW"))
  374. {
  375. SET_BIT(ses->log->mode, LOG_FLAG_RAW);
  376. DEL_BIT(ses->log->mode, LOG_FLAG_HTML);
  377. DEL_BIT(ses->log->mode, LOG_FLAG_PLAIN);
  378. }
  379. else if (is_abbrev(arg1, "STAMP"))
  380. {
  381. SET_BIT(ses->log->mode, LOG_FLAG_STAMP);
  382. }
  383. else
  384. {
  385. show_error(ses, LIST_COMMAND, "#SYNTAX: #LINE {LOGMODE} {HTML|PLAIN|RAW|STAMP} {command}.");
  386. return ses;
  387. }
  388. arg = get_arg_in_braces(ses, arg, arg1, GET_ALL);
  389. active_ses = script_driver(ses, LIST_COMMAND, arg1);
  390. ses->log->mode = old_mode;
  391. return active_ses;
  392. }
  393. DO_LINE(line_msdp)
  394. {
  395. arg = sub_arg_in_braces(ses, arg, arg1, GET_ALL, SUB_VAR|SUB_FUN);
  396. if (*arg1 == 0)
  397. {
  398. show_error(ses, LIST_COMMAND, "#SYNTAX: #LINE {MSDP} {command}.");
  399. return ses;
  400. }
  401. tintin2msdp(arg1, arg2);
  402. ses = script_driver(ses, LIST_COMMAND, arg2);
  403. return ses;
  404. }
  405. DO_LINE(line_json)
  406. {
  407. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  408. arg = sub_arg_in_braces(ses, arg, arg2, GET_ALL, SUB_VAR|SUB_FUN);
  409. if (*arg1 == 0)
  410. {
  411. show_error(ses, LIST_COMMAND, "#SYNTAX: #LINE {JSON} {command}.");
  412. return ses;
  413. }
  414. char *str_sub = str_alloc_stack(0);
  415. // struct listroot *root = ses->list[LIST_VARIABLE];
  416. struct listnode *node = search_nest_node_ses(ses, arg1);
  417. if (node)
  418. {
  419. if (node->root)
  420. {
  421. view_nest_node_json(node, &str_sub, 0, TRUE);
  422. // print_lines(ses, SUB_NONE, "", "%s\n", str_sub);
  423. }
  424. else
  425. {
  426. sprintf(str_sub, "\"%s\"\n", node->arg2);
  427. }
  428. }
  429. else
  430. {
  431. show_error(ses, LIST_COMMAND, "#LINE JSON {%s}: VARIABLE NOT FOUND.", arg1);
  432. }
  433. RESTRING(gtd->cmds[0], str_sub);
  434. substitute(ses, arg2, str_sub, SUB_CMD);
  435. ses = script_driver(ses, LIST_COMMAND, str_sub);
  436. return ses;
  437. }
  438. DO_LINE(line_multishot)
  439. {
  440. unsigned int shots;
  441. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  442. arg = get_arg_in_braces(ses, arg, arg2, GET_ALL);
  443. shots = (unsigned int) get_number(ses, arg1);
  444. if (!is_math(ses, arg1) || *arg2 == 0)
  445. {
  446. show_error(ses, LIST_COMMAND, "#SYNTAX: #LINE {MULTISHOT} {number} {command}.");
  447. return ses;
  448. }
  449. gtd->level->shots++;
  450. gtd->level->mshot = shots;
  451. ses = script_driver(ses, LIST_COMMAND, arg2);
  452. gtd->level->mshot = 1;
  453. gtd->level->shots--;
  454. return ses;
  455. }
  456. DO_LINE(line_oneshot)
  457. {
  458. arg = get_arg_in_braces(ses, arg, arg1, GET_ALL);
  459. if (*arg1 == 0)
  460. {
  461. show_error(ses, LIST_COMMAND, "#SYNTAX: #LINE {ONESHOT} {command}.");
  462. return ses;
  463. }
  464. gtd->level->shots++;
  465. gtd->level->mshot = 1;
  466. ses = script_driver(ses, LIST_COMMAND, arg1);
  467. gtd->level->shots--;
  468. return ses;
  469. }
  470. DO_LINE(line_quiet)
  471. {
  472. arg = get_arg_in_braces(ses, arg, arg1, GET_ALL);
  473. if (*arg1 == 0)
  474. {
  475. show_error(ses, LIST_COMMAND, "#SYNTAX: #LINE {QUIET} {command}.");
  476. return ses;
  477. }
  478. gtd->level->quiet++;
  479. ses = script_driver(ses, LIST_COMMAND, arg1);
  480. gtd->level->quiet--;
  481. return ses;
  482. }
  483. DO_LINE(line_strip)
  484. {
  485. arg = sub_arg_in_braces(ses, arg, arg1, GET_ALL, SUB_ESC|SUB_COL);
  486. if (*arg1 == 0)
  487. {
  488. show_error(ses, LIST_COMMAND, "#SYNTAX: #LINE {STRIP} {command}.");
  489. return ses;
  490. }
  491. strip_vt102_codes(arg1, arg2);
  492. ses = script_driver(ses, LIST_COMMAND, arg2);
  493. return ses;
  494. }
  495. DO_LINE(line_substitute)
  496. {
  497. int i, flags = 0;
  498. arg = get_arg_in_braces(ses, arg, arg1, GET_ONE);
  499. arg = get_arg_in_braces(ses, arg, arg2, GET_ALL);
  500. if (*arg2 == 0)
  501. {
  502. for (i = 0 ; *substitution_table[i].name ; i++)
  503. {
  504. tintin_printf2(ses, "#SYNTAX: #LINE {SUBSTITUTE} {%s} {command}.", substitution_table[i].name);
  505. }
  506. return ses;
  507. }
  508. arg = arg1;
  509. while (*arg)
  510. {
  511. arg = get_arg_in_braces(ses, arg, arg3, GET_ONE);
  512. for (i = 0 ; *substitution_table[i].name ; i++)
  513. {
  514. if (is_abbrev(arg3, substitution_table[i].name))
  515. {
  516. SET_BIT(flags, substitution_table[i].bitvector);
  517. }
  518. }
  519. if (*arg == COMMAND_SEPARATOR)
  520. {
  521. arg++;
  522. }
  523. }
  524. substitute(ses, arg2, arg3, flags);
  525. ses = script_driver(ses, LIST_COMMAND, arg3);
  526. return ses;
  527. }
  528. DO_LINE(line_verbatim)
  529. {
  530. arg = get_arg_in_braces(ses, arg, arg1, GET_ALL);
  531. if (*arg1 == 0)
  532. {
  533. show_error(ses, LIST_COMMAND, "#SYNTAX: #LINE {VERBATIM} {command}.");
  534. return ses;
  535. }
  536. gtd->level->verbatim++;
  537. ses = parse_input(ses, arg1);
  538. gtd->level->verbatim--;
  539. return ses;
  540. }
  541. DO_LINE(line_verbose)
  542. {
  543. arg = get_arg_in_braces(ses, arg, arg1, GET_ALL);
  544. if (*arg1 == 0)
  545. {
  546. show_error(ses, LIST_COMMAND, "#SYNTAX: #LINE {VERBOSE} {command}.");
  547. return ses;
  548. }
  549. gtd->level->verbose++;
  550. ses = script_driver(ses, LIST_COMMAND, arg1);
  551. gtd->level->verbose--;
  552. return ses;
  553. }