line.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 Igor van den Hoven 2008 *
  24. ******************************************************************************/
  25. #include "tintin.h"
  26. DO_COMMAND(do_line)
  27. {
  28. char arg1[BUFFER_SIZE];
  29. int cnt;
  30. arg = get_arg_in_braces(ses, arg, arg1, GET_ONE);
  31. if (*arg1 == 0)
  32. {
  33. show_error(ses, LIST_COMMAND, "#SYNTAX: #LINE {<OPTION>} {argument}.");
  34. }
  35. else
  36. {
  37. for (cnt = 0 ; *line_table[cnt].name ; cnt++)
  38. {
  39. if (is_abbrev(arg1, line_table[cnt].name))
  40. {
  41. break;
  42. }
  43. }
  44. if (*line_table[cnt].name == 0)
  45. {
  46. do_line(ses, "");
  47. }
  48. else
  49. {
  50. ses = line_table[cnt].fun(ses, arg);
  51. }
  52. }
  53. return ses;
  54. }
  55. DO_LINE(line_gag)
  56. {
  57. char arg1[BUFFER_SIZE];
  58. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  59. show_debug(ses, LIST_GAG, "#DEBUG LINE GAG");
  60. SET_BIT(ses->flags, SES_FLAG_GAG);
  61. return ses;
  62. }
  63. // Without an argument mark next line to be logged, otherwise log the given line to file.
  64. DO_LINE(line_log)
  65. {
  66. char arg1[BUFFER_SIZE], arg2[BUFFER_SIZE];
  67. FILE *logfile;
  68. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  69. arg = sub_arg_in_braces(ses, arg, arg2, GET_ALL, SUB_VAR|SUB_FUN);
  70. if (*arg1 && *arg2)
  71. {
  72. substitute(ses, arg2, arg2, SUB_ESC|SUB_COL|SUB_LNF);
  73. if (ses->logline_time == gtd->time && !strcmp(ses->logline_name, arg1))
  74. {
  75. logit(ses, arg2, ses->logline_file, FALSE);
  76. }
  77. else
  78. {
  79. if ((logfile = fopen(arg1, "a")))
  80. {
  81. if (ses->logline_file)
  82. {
  83. fclose(ses->logline_file);
  84. }
  85. free(ses->logline_name);
  86. ses->logline_name = strdup(arg1);
  87. ses->logline_file = logfile;
  88. ses->logline_time = gtd->time;
  89. if (HAS_BIT(ses->flags, SES_FLAG_LOGHTML))
  90. {
  91. fseek(logfile, 0, SEEK_END);
  92. if (ftell(logfile) == 0)
  93. {
  94. write_html_header(ses, logfile);
  95. }
  96. }
  97. logit(ses, arg2, ses->logline_file, FALSE);
  98. }
  99. else
  100. {
  101. show_error(ses, LIST_COMMAND, "#ERROR: #LINE LOG {%s} - COULDN'T OPEN FILE.", arg1);
  102. }
  103. }
  104. }
  105. else
  106. {
  107. if (ses->lognext_time == gtd->time && !strcmp(ses->lognext_name, arg1))
  108. {
  109. SET_BIT(ses->flags, SES_FLAG_LOGNEXT);
  110. }
  111. else if ((logfile = fopen(arg1, "a")))
  112. {
  113. if (ses->lognext_file)
  114. {
  115. fclose(ses->lognext_file);
  116. }
  117. free(ses->lognext_name);
  118. ses->lognext_name = strdup(arg1);
  119. ses->lognext_file = logfile;
  120. ses->lognext_time = gtd->time;
  121. SET_BIT(ses->flags, SES_FLAG_LOGNEXT);
  122. }
  123. else
  124. {
  125. show_error(ses, LIST_COMMAND, "#ERROR: #LINE LOG {%s} - COULDN'T OPEN FILE.", arg1);
  126. }
  127. }
  128. return ses;
  129. }
  130. DO_LINE(line_logverbatim)
  131. {
  132. char arg1[BUFFER_SIZE], arg2[BUFFER_SIZE];
  133. FILE *logfile;
  134. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  135. arg = get_arg_in_braces(ses, arg, arg2, GET_ALL);
  136. if (*arg1 && *arg2)
  137. {
  138. if (!strcmp(ses->logline_name, arg1))
  139. {
  140. logit(ses, arg2, ses->logline_file, TRUE);
  141. }
  142. else
  143. {
  144. if ((logfile = fopen(arg1, "a")))
  145. {
  146. if (ses->logline_file)
  147. {
  148. fclose(ses->logline_file);
  149. }
  150. free(ses->logline_name);
  151. ses->logline_name = strdup(arg1);
  152. ses->logline_file = logfile;
  153. if (HAS_BIT(ses->flags, SES_FLAG_LOGHTML))
  154. {
  155. fseek(logfile, 0, SEEK_END);
  156. if (ftell(logfile) == 0)
  157. {
  158. write_html_header(ses, logfile);
  159. }
  160. }
  161. logit(ses, arg2, ses->logline_file, TRUE);
  162. }
  163. else
  164. {
  165. show_error(ses, LIST_COMMAND, "#ERROR: #LINE LOGVERBATIM {%s} - COULDN'T OPEN FILE.", arg1);
  166. }
  167. }
  168. }
  169. else
  170. {
  171. if (!strcmp(ses->lognext_name, arg1))
  172. {
  173. SET_BIT(ses->flags, SES_FLAG_LOGNEXT);
  174. }
  175. else if ((logfile = fopen(arg1, "a")))
  176. {
  177. if (ses->lognext_file)
  178. {
  179. fclose(ses->lognext_file);
  180. }
  181. free(ses->lognext_name);
  182. ses->lognext_name = strdup(arg1);
  183. ses->lognext_file = logfile;
  184. SET_BIT(ses->flags, SES_FLAG_LOGNEXT);
  185. }
  186. else
  187. {
  188. show_error(ses, LIST_COMMAND, "#ERROR: #LINE LOGVERBATIM {%s} - COULDN'T OPEN FILE.", arg1);
  189. }
  190. }
  191. return ses;
  192. }
  193. DO_LINE(line_strip)
  194. {
  195. char arg1[BUFFER_SIZE], strip[BUFFER_SIZE];
  196. arg = sub_arg_in_braces(ses, arg, arg1, GET_ALL, SUB_ESC|SUB_COL);
  197. if (*arg1 == 0)
  198. {
  199. show_error(ses, LIST_COMMAND, "#SYNTAX: #LINE {STRIP} {command}.");
  200. return ses;
  201. }
  202. strip_vt102_codes(arg1, strip);
  203. ses = script_driver(ses, LIST_COMMAND, strip);
  204. return ses;
  205. }
  206. DO_LINE(line_substitute)
  207. {
  208. char arg1[BUFFER_SIZE], arg2[BUFFER_SIZE], subs[BUFFER_SIZE];
  209. int i, flags = 0;
  210. arg = get_arg_in_braces(ses, arg, arg1, GET_ONE);
  211. arg = get_arg_in_braces(ses, arg, arg2, GET_ALL);
  212. if (*arg2 == 0)
  213. {
  214. show_error(ses, LIST_COMMAND, "#SYNTAX: #LINE {SUBSTITUTE} {argument} {command}.");
  215. return ses;
  216. }
  217. arg = arg1;
  218. while (*arg)
  219. {
  220. arg = get_arg_in_braces(ses, arg, subs, GET_ONE);
  221. for (i = 0 ; *substitution_table[i].name ; i++)
  222. {
  223. if (is_abbrev(subs, substitution_table[i].name))
  224. {
  225. SET_BIT(flags, substitution_table[i].bitvector);
  226. }
  227. }
  228. if (*arg == COMMAND_SEPARATOR)
  229. {
  230. arg++;
  231. }
  232. }
  233. substitute(ses, arg2, subs, flags);
  234. ses = script_driver(ses, LIST_COMMAND, subs);
  235. return ses;
  236. }
  237. DO_LINE(line_verbatim)
  238. {
  239. char arg1[BUFFER_SIZE];
  240. arg = get_arg_in_braces(ses, arg, arg1, GET_ALL);
  241. if (*arg1 == 0)
  242. {
  243. show_error(ses, LIST_COMMAND, "#SYNTAX: #LINE {VERBATIM} {command}.");
  244. return ses;
  245. }
  246. gtd->verbatim_level++;
  247. ses = parse_input(ses, arg1);
  248. gtd->verbatim_level--;
  249. return ses;
  250. }
  251. DO_LINE(line_verbose)
  252. {
  253. char arg1[BUFFER_SIZE];
  254. arg = get_arg_in_braces(ses, arg, arg1, GET_ALL);
  255. if (*arg1 == 0)
  256. {
  257. show_error(ses, LIST_COMMAND, "#SYNTAX: #LINE {VERBOSE} {command}.");
  258. return ses;
  259. }
  260. gtd->verbose_level++;
  261. ses = script_driver(ses, LIST_COMMAND, arg1);
  262. gtd->verbose_level--;
  263. return ses;
  264. }
  265. DO_LINE(line_ignore)
  266. {
  267. char arg1[BUFFER_SIZE];
  268. arg = get_arg_in_braces(ses, arg, arg1, GET_ALL);
  269. if (*arg1 == 0)
  270. {
  271. show_error(ses, LIST_COMMAND, "#SYNTAX: #LINE {IGNORE} {command}.");
  272. return ses;
  273. }
  274. gtd->ignore_level++;
  275. ses = script_driver(ses, LIST_COMMAND, arg1);
  276. gtd->ignore_level--;
  277. return ses;
  278. }
  279. DO_LINE(line_quiet)
  280. {
  281. char arg1[BUFFER_SIZE];
  282. arg = get_arg_in_braces(ses, arg, arg1, GET_ALL);
  283. if (*arg1 == 0)
  284. {
  285. show_error(ses, LIST_COMMAND, "#SYNTAX: #LINE {QUIET} {command}.");
  286. return ses;
  287. }
  288. gtd->quiet++;
  289. ses = script_driver(ses, LIST_COMMAND, arg1);
  290. gtd->quiet--;
  291. return ses;
  292. }