4
0

slang_text.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (C) 2000,2001 Florian Sander
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. struct slang_text {
  19. struct slang_text *next;
  20. char *string;
  21. void (*command) ();
  22. };
  23. struct slang_text_commands {
  24. char *command;
  25. void (*targetfunc) ();
  26. };
  27. struct slang_command_list {
  28. struct slang_command_list *next;
  29. struct slang_text_commands *commands;
  30. };
  31. static struct slang_text *slang_text_parse(char *);
  32. static struct slang_text *slang_text_create(struct slang_text *);
  33. static void slang_text_add_string(struct slang_text *, char *);
  34. static void slang_text_add_command(struct slang_text *, char *);
  35. static void slang_text_free(struct slang_text *);
  36. //static int slang_text_expmem(struct slang_text *);
  37. static char *slang_text_get(struct slang_text *);
  38. #ifndef SLANG_NOTYPES
  39. static int slang_text_strcasecmp(struct slang_text *, char *);
  40. #endif
  41. static struct slang_text *slang_text_parse(char *text)
  42. {
  43. char *cmdstart, *cmdend;
  44. struct slang_text *firstitem, *item;
  45. firstitem = slang_text_create(NULL);
  46. item = firstitem;
  47. while ((cmdstart = strstr(text, "<?"))) {
  48. cmdstart[0] = 0;
  49. slang_text_add_string(item, text);
  50. item = slang_text_create(item);
  51. text += 2;
  52. cmdstart += 2;
  53. cmdend = strstr(cmdstart, "/?>");
  54. if (!cmdend) {
  55. putlog(LOG_MISC, "*", "ERROR parsing slang text: unterminated command \"%s\"!", cmdstart);
  56. break;
  57. }
  58. cmdend[0] = 0;
  59. slang_text_add_command(item, cmdstart);
  60. item = slang_text_create(item);
  61. text = cmdend + 3;
  62. }
  63. slang_text_add_string(item, text);
  64. return firstitem;
  65. }
  66. static struct slang_text *slang_text_create(struct slang_text *where)
  67. {
  68. struct slang_text *newpart;
  69. newpart = nmalloc(sizeof(struct slang_text));
  70. newpart->next = NULL;
  71. newpart->string = NULL;
  72. newpart->command = NULL;
  73. while (where && where->next)
  74. where = where->next;
  75. if (where)
  76. where->next = newpart;
  77. return newpart;
  78. }
  79. static void slang_text_add_string(struct slang_text *item, char *s)
  80. {
  81. Assert(item);
  82. Assert(!item->string);
  83. item->string = nmalloc(strlen(s) + 1);
  84. strcpy(item->string, s);
  85. }
  86. static void slang_text_free(struct slang_text *item)
  87. {
  88. if (!item)
  89. return;
  90. slang_text_free(item->next);
  91. if (item->string)
  92. nfree(item->string);
  93. nfree(item);
  94. }
  95. /*static int slang_text_expmem(struct slang_text *item)
  96. {
  97. int size = 0;
  98. while (item) {
  99. size += sizeof(struct slang_text);
  100. if (item->string)
  101. size += strlen(item->string) + 1;
  102. item = item->next;
  103. }
  104. return size;
  105. }*/
  106. #ifndef SLANG_NOTYPES
  107. static int slang_text_strcasecmp(struct slang_text *item, char *text)
  108. {
  109. Assert(item);
  110. debug2("s_t_sc: '%s', '%s'", text, item->string);
  111. if (item->command || item->next)
  112. return 1;
  113. return strcasecmp(item->string, text);
  114. }
  115. #endif
  116. static char slang_text_buf[500];
  117. static char *slang_text_get(struct slang_text *item)
  118. {
  119. slang_text_buf[0] = 0;
  120. while (item) {
  121. if (item->string)
  122. strncat(slang_text_buf, item->string, sizeof(slang_text_buf));
  123. else if (item->command)
  124. item->command();
  125. item = item->next;
  126. }
  127. return slang_text_buf;
  128. }
  129. /*****************************************************/
  130. static struct slang_command_list *glob_slang_cmd_list;
  131. static struct slang_command_list *slang_commands_list_add(struct slang_command_list *where, struct slang_text_commands *what)
  132. {
  133. struct slang_command_list *newcommandlist;
  134. newcommandlist = nmalloc(sizeof(struct slang_command_list));
  135. newcommandlist->commands = what;
  136. newcommandlist->next = where;
  137. return newcommandlist;
  138. }
  139. /*
  140. static int slang_commands_list_expmem(struct slang_command_list *what)
  141. {
  142. int size = 0;
  143. while (what) {
  144. size += sizeof(struct slang_command_list);
  145. what = what->next;
  146. }
  147. return size;
  148. }
  149. */
  150. static void slang_commands_list_free(struct slang_command_list *what)
  151. {
  152. struct slang_command_list *next;
  153. while (what) {
  154. next = what->next;
  155. nfree(what);
  156. what = next;
  157. }
  158. }
  159. static void slang_text_add_command(struct slang_text *item, char *s)
  160. {
  161. struct slang_command_list *cmdlist;
  162. char *cmd;
  163. int i;
  164. cmd = newsplit(&s);
  165. i = 0;
  166. for (cmdlist = glob_slang_cmd_list; cmdlist; cmdlist = cmdlist->next) {
  167. for (i = 0; 1; i++) {
  168. if (!cmdlist->commands[i].command)
  169. break;
  170. if (!strcasecmp(cmdlist->commands[i].command, cmd)) {
  171. item->command = cmdlist->commands[i].targetfunc;
  172. return;
  173. }
  174. }
  175. }
  176. putlog(LOG_MISC, "*", "ERROR! Unknown slang-command: '%s'", cmd);
  177. }