| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- /******************************************************************************
- * This file is part of TinTin++ *
- * *
- * Copyright 2004-2020 Igor van den Hoven *
- * *
- * TinTin++ is free software; you can redistribute it and/or modify *
- * it under the terms of the GNU General Public License as published by *
- * the Free Software Foundation; either version 3 of the License, or *
- * (at your option) any later version. *
- * *
- * This program is distributed in the hope that it will be useful, *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
- * GNU General Public License for more details. *
- * *
- * You should have received a copy of the GNU General Public License *
- * along with TinTin++. If not, see https://www.gnu.org/licenses. *
- ******************************************************************************/
- /******************************************************************************
- * T I N T I N + + *
- * *
- * coded by Igor van den Hoven 2006 *
- ******************************************************************************/
- #include "tintin.h"
- DO_COMMAND(do_history)
- {
- int cnt;
- arg = get_arg_in_braces(ses, arg, arg1, GET_ONE);
- if (*arg1 == 0)
- {
- info:
- tintin_header(ses, 80, " HISTORY COMMANDS ");
- for (cnt = 0 ; *history_table[cnt].name != 0 ; cnt++)
- {
- tintin_printf2(ses, " [%-13s] %s", history_table[cnt].name, history_table[cnt].desc);
- }
- tintin_header(ses, 80, "");
- return ses;
- }
- for (cnt = 0 ; *history_table[cnt].name ; cnt++)
- {
- if (!is_abbrev(arg1, history_table[cnt].name))
- {
- continue;
- }
- history_table[cnt].fun(ses, arg, arg2, arg3);
- return ses;
- }
- goto info;
- return ses;
- }
- void add_line_history(struct session *ses, char *line)
- {
- struct listroot *root;
- int last;
- root = ses->list[LIST_HISTORY];
- if (HAS_BIT(root->flags, LIST_FLAG_IGNORE) || gtd->level->ignore)
- {
- return;
- }
- // avoid infinite loops
- if (*line == gtd->repeat_char)
- {
- return;
- }
- last = root->used;
- update_node_list(ses->list[LIST_HISTORY], line, "", "", "");
- if (last < root->used)
- {
- SET_BIT(gtd->flags, TINTIN_FLAG_HISTORYUPDATE);
- }
- while (root->used > gtd->history_size)
- {
- delete_index_list(ses->list[LIST_HISTORY], 0);
- }
- return;
- }
- struct session *repeat_history(struct session *ses, char *line)
- {
- struct listroot *root = ses->list[LIST_HISTORY];
- int i;
- for (i = root->used - 1 ; i >= 0 ; i--)
- {
- if (!strncmp(root->list[i]->arg1, line, strlen(line)))
- {
- add_line_history(gtd->ses, root->list[i]->arg1);
- gtd->level->repeat++;
- ses = script_driver(ses, LIST_COMMAND, root->list[root->used - 1]->arg1);
- gtd->level->repeat--;
- return ses;
- }
- }
- tintin_printf2(ses, "#REPEAT: NO MATCH FOUND FOR '%s'", line);
- return ses;
- }
- DO_HISTORY(history_character)
- {
- arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
- gtd->repeat_char = *arg1;
- show_message(ses, LIST_HISTORY, "#HISTORY CHARACTER SET TO {%c}.", gtd->repeat_char);
- }
- DO_HISTORY(history_delete)
- {
- if (ses->list[LIST_HISTORY]->used)
- {
- delete_index_list(ses->list[LIST_HISTORY], ses->list[LIST_HISTORY]->used - 1);
- }
- return;
- }
- DO_HISTORY(history_insert)
- {
- arg = get_arg_in_braces(ses, arg, arg1, GET_ALL);
- add_line_history(ses, arg1);
- }
- DO_HISTORY(history_get)
- {
- struct listroot *root = ses->list[LIST_HISTORY];
- char *arg3;
- int cnt, min, max;
- arg = get_arg_in_braces(ses, arg, arg1, GET_ONE);
- if (*arg1 == 0)
- {
- show_error(ses, LIST_COMMAND, "#SYNTAX: #HISTORY GET <VARIABLE> [LOWER BOUND] [UPPER BOUND]");
- return;
- }
- arg = get_arg_in_braces(ses, arg, arg2, GET_ONE);
- min = get_number(ses, arg2);
- if (min < 0)
- {
- min = root->used + min;
- }
- min = URANGE(0, min, root->used - 1);
- arg3 = str_alloc_stack(0);
- arg = sub_arg_in_braces(ses, arg, arg3, GET_ONE, SUB_VAR|SUB_FUN);
- if (*arg3 == 0)
- {
- substitute(ses, root->list[min]->arg1, arg3, SUB_SEC);
- set_nest_node_ses(ses, arg1, "%s", arg3);
- return;
- }
- max = get_number(ses, arg3);
- if (max < 0)
- {
- max = root->used + max;
- }
- max = URANGE(0, max, root->used - 1);
- if (min > max)
- {
- show_error(ses, LIST_COMMAND, "#ERROR: #HISTORY GET {%s} {%d} {%d} LOWER BOUND EXCEEDS UPPER BOUND.", arg1, min, max);
- return;
- }
- cnt = 0;
- set_nest_node_ses(ses, arg1, "");
- while (min <= max)
- {
- sprintf(arg2, "%s[%d]", arg1, ++cnt);
- substitute(ses, root->list[min++]->arg1, arg3, SUB_SEC);
- set_nest_node_ses(ses, arg2, "%s", arg3);
- }
- show_message(ses, LIST_COMMAND, "#HISTORY GET: %d LINES SAVED TO {%s}.", cnt, arg1);
- return;
- }
- DO_HISTORY(history_list)
- {
- arg = sub_arg_in_braces(ses, arg, arg1, GET_ALL, SUB_VAR|SUB_FUN);
- if (*arg1 == 0)
- {
- show_list(ses->list[LIST_HISTORY], 0);
- }
- else
- {
- show_node_with_wild(ses, arg1, ses->list[LIST_HISTORY]);
- }
- return;
- }
- DO_HISTORY(history_read)
- {
- struct listroot *root = ses->list[LIST_HISTORY];
- FILE *file;
- arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
- file = fopen(arg1, "r");
- if (file == NULL)
- {
- show_message(ses, LIST_HISTORY, "#HISTORY: COULDN'T OPEN FILE {%s} TO READ.", arg1);
- return;
- }
- kill_list(root);
- while (fread_one_line(&arg2, file))
- {
- if (*arg2)
- {
- create_node_list(root, arg2, "", "", "");
- }
- }
- create_node_list(root, "", "", "", "");
- fclose(file);
- if (ses->list[LIST_HISTORY]->used > gtd->history_size)
- {
- command(gts, do_configure, "{HISTORY SIZE} {%d}", UMIN(ses->list[LIST_HISTORY]->used, 9999));
- }
- return;
- }
- DO_HISTORY(history_size)
- {
- arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
- if (atoi(arg1) < 1 || atoi(arg1) > 100000)
- {
- show_error(ses, LIST_COMMAND, "#ERROR: #HISTORY SIZE: PROVIDE A NUMBER BETWEEN 1 and 100,000");
- }
- else
- {
- gtd->history_size = atoi(arg1);
- }
- return;
- }
- DO_HISTORY(history_write)
- {
- struct listroot *root = ses->list[LIST_HISTORY];
- FILE *file;
- int i;
- arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
- file = fopen(arg1, "w");
- if (file == NULL)
- {
- tintin_printf2(ses, "#HISTORY: COULDN'T OPEN FILE {%s} TO WRITE.", arg1);
- return;
- }
- for (i = 0 ; i < root->used ; i++)
- {
- fprintf(file, "%s\n", root->list[i]->arg1);
- }
- fclose(file);
- return;
- }
|