history.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 2006 *
  23. ******************************************************************************/
  24. #include "tintin.h"
  25. DO_COMMAND(do_history)
  26. {
  27. char arg1[BUFFER_SIZE], arg2[BUFFER_SIZE];
  28. int cnt;
  29. arg = get_arg_in_braces(ses, arg, arg1, GET_ONE);
  30. arg = sub_arg_in_braces(ses, arg, arg2, GET_ALL, SUB_VAR|SUB_FUN);
  31. if (*arg1 == 0)
  32. {
  33. tintin_header(ses, " HISTORY COMMANDS ");
  34. for (cnt = 0 ; *history_table[cnt].name != 0 ; cnt++)
  35. {
  36. tintin_printf2(ses, " [%-13s] %s", history_table[cnt].name, history_table[cnt].desc);
  37. }
  38. tintin_header(ses, "");
  39. return ses;
  40. }
  41. for (cnt = 0 ; *history_table[cnt].name ; cnt++)
  42. {
  43. if (!is_abbrev(arg1, history_table[cnt].name))
  44. {
  45. continue;
  46. }
  47. history_table[cnt].fun(ses, arg2);
  48. return ses;
  49. }
  50. do_history(ses, "");
  51. return ses;
  52. }
  53. void add_line_history(struct session *ses, char *line)
  54. {
  55. struct listroot *root;
  56. root = ses->list[LIST_HISTORY];
  57. if (HAS_BIT(root->flags, LIST_FLAG_IGNORE) || gtd->level->ignore)
  58. {
  59. return;
  60. }
  61. if (*line == 0)
  62. {
  63. if (root->used && HAS_BIT(ses->flags, SES_FLAG_REPEATENTER))
  64. {
  65. strcpy(line, root->list[root->used - 1]->arg1);
  66. }
  67. return;
  68. }
  69. if (*line == gtd->repeat_char)
  70. {
  71. search_line_history(ses, line);
  72. }
  73. update_node_list(ses->list[LIST_HISTORY], line, "", "", "");
  74. while (root->used > gtd->history_size)
  75. {
  76. delete_index_list(ses->list[LIST_HISTORY], 0);
  77. }
  78. return;
  79. }
  80. void search_line_history(struct session *ses, char *line)
  81. {
  82. struct listroot *root;
  83. int i;
  84. root = ses->list[LIST_HISTORY];
  85. for (i = root->used - 1 ; i >= 0 ; i--)
  86. {
  87. if (!strncmp(root->list[i]->arg1, &line[1], strlen(&line[1])))
  88. {
  89. strcpy(line, root->list[i]->arg1);
  90. return;
  91. }
  92. }
  93. tintin_printf2(ses, "#REPEAT: NO MATCH FOUND FOR '%s'", line);
  94. }
  95. DO_HISTORY(history_character)
  96. {
  97. gtd->repeat_char = *arg;
  98. show_message(ses, LIST_HISTORY, "#HISTORY CHARACTER SET TO {%c}.", gtd->repeat_char);
  99. }
  100. DO_HISTORY(history_delete)
  101. {
  102. if (ses->list[LIST_HISTORY]->used)
  103. {
  104. delete_index_list(ses->list[LIST_HISTORY], ses->list[LIST_HISTORY]->used - 1);
  105. }
  106. return;
  107. }
  108. DO_HISTORY(history_insert)
  109. {
  110. add_line_history(ses, arg);
  111. }
  112. DO_HISTORY(history_list)
  113. {
  114. struct listroot *root;
  115. int i, cnt = 1;
  116. root = ses->list[LIST_HISTORY];
  117. for (i = 0 ; i < root->used ; i++)
  118. {
  119. tintin_printf2(ses, "%6d - %s", cnt++, root->list[i]->arg1);
  120. }
  121. return;
  122. }
  123. DO_HISTORY(history_read)
  124. {
  125. FILE *file;
  126. char *cptr, buffer[BUFFER_SIZE];
  127. file = fopen(arg, "r");
  128. if (file == NULL)
  129. {
  130. show_message(ses, LIST_HISTORY, "#HISTORY: COULDN'T OPEN FILE {%s} TO READ.", arg);
  131. return;
  132. }
  133. kill_list(ses->list[LIST_HISTORY]);
  134. while (fgets(buffer, BUFFER_SIZE-1, file))
  135. {
  136. cptr = strchr(buffer, '\n');
  137. if (cptr)
  138. {
  139. *cptr = 0;
  140. if (*buffer)
  141. {
  142. insert_node_list(ses->list[LIST_HISTORY], buffer, "", "", "");
  143. }
  144. }
  145. }
  146. insert_node_list(ses->list[LIST_HISTORY], "", "", "", "");
  147. fclose(file);
  148. if (ses->list[LIST_HISTORY]->used > gtd->history_size)
  149. {
  150. char buf[BUFFER_SIZE];
  151. sprintf(buf, "{HISTORY SIZE} {%d}", UMIN(ses->list[LIST_HISTORY]->used, 9999));
  152. do_configure(gts, buf);
  153. }
  154. return;
  155. }
  156. DO_HISTORY(history_size)
  157. {
  158. if (atoi(arg) < 1 || atoi(arg) > 100000)
  159. {
  160. show_error(ses, LIST_COMMAND, "#ERROR: #HISTORY SIZE: PROVIDE A NUMBER BETWEEN 1 and 100,000");
  161. }
  162. else
  163. {
  164. gtd->history_size = atoi(arg);
  165. }
  166. return;
  167. }
  168. DO_HISTORY(history_write)
  169. {
  170. struct listroot *root = ses->list[LIST_HISTORY];
  171. FILE *file;
  172. int i;
  173. file = fopen(arg, "w");
  174. if (file == NULL)
  175. {
  176. tintin_printf2(ses, "#HISTORY: COULDN'T OPEN FILE {%s} TO WRITE.", arg);
  177. return;
  178. }
  179. for (i = 0 ; i < root->used ; i++)
  180. {
  181. fprintf(file, "%s\n", root->list[i]->arg1);
  182. }
  183. fclose(file);
  184. return;
  185. }