substitute.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. ******************************************************************************/
  25. #include "tintin.h"
  26. DO_COMMAND(do_substitute)
  27. {
  28. char arg1[BUFFER_SIZE], arg2[BUFFER_SIZE], arg3[BUFFER_SIZE], *str;
  29. str = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  30. arg = get_arg_in_braces(ses, str, arg2, GET_ALL);
  31. arg = get_arg_in_braces(ses, arg, arg3, GET_ALL);
  32. if (*arg3 == 0)
  33. {
  34. strcpy(arg3, "5");
  35. }
  36. if (*arg1 == 0)
  37. {
  38. show_list(ses->list[LIST_SUBSTITUTE], 0);
  39. }
  40. else if (*str == 0)
  41. {
  42. if (show_node_with_wild(ses, arg1, ses->list[LIST_SUBSTITUTE]) == FALSE)
  43. {
  44. show_message(ses, LIST_SUBSTITUTE, "#SUBSTITUTE: NO MATCH(ES) FOUND FOR {%s}.", arg1);
  45. }
  46. }
  47. else
  48. {
  49. update_node_list(ses->list[LIST_SUBSTITUTE], arg1, arg2, arg3, "");
  50. show_message(ses, LIST_SUBSTITUTE, "#OK. {%s} IS NOW SUBSTITUTED AS {%s} @ {%s}.", arg1, arg2, arg3);
  51. }
  52. return ses;
  53. }
  54. DO_COMMAND(do_unsubstitute)
  55. {
  56. delete_node_with_wild(ses, LIST_SUBSTITUTE, arg);
  57. return ses;
  58. }
  59. void check_all_substitutions(struct session *ses, char *original, char *line)
  60. {
  61. char match[BUFFER_SIZE], subst[BUFFER_SIZE], output[BUFFER_SIZE], temp[BUFFER_SIZE], *ptl, *ptm, *pto;
  62. struct listroot *root = ses->list[LIST_SUBSTITUTE];
  63. struct listnode *node;
  64. int len;
  65. for (root->update = 0 ; root->update < root->used ; root->update++)
  66. {
  67. if (check_one_regexp(ses, root->list[root->update], line, original, 0))
  68. {
  69. node = root->list[root->update];
  70. pto = original;
  71. ptl = line;
  72. *output = 0;
  73. do
  74. {
  75. if (*gtd->vars[0] == 0)
  76. {
  77. break;
  78. }
  79. strcpy(match, gtd->vars[0]);
  80. substitute(ses, node->arg2, temp, SUB_ARG);
  81. substitute(ses, temp, subst, SUB_VAR|SUB_FUN|SUB_COL|SUB_ESC);
  82. if (*node->arg1 == '~')
  83. {
  84. ptm = strstr(pto, match);
  85. len = strlen(match);
  86. }
  87. else
  88. {
  89. ptm = strip_vt102_strstr(pto, match, &len);
  90. ptl = strstr(ptl, match) + strlen(match);
  91. }
  92. *ptm = 0;
  93. cat_sprintf(output, "%s%s", pto, subst);
  94. pto = ptm + len;
  95. show_debug(ses, LIST_SUBSTITUTE, "#DEBUG SUBSTITUTE {%s} {%s}", node->arg1, match);
  96. }
  97. while (check_one_regexp(ses, node, ptl, pto, 0));
  98. strcat(output, pto);
  99. // substitute(ses, output, original, SUB_VAR|SUB_FUN|SUB_COL|SUB_ESC);
  100. strcpy(original, output);
  101. strip_vt102_codes(original, line);
  102. }
  103. }
  104. }