templates.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. #include "templates_commands.c"
  19. #include "templates_content.c"
  20. #include "templates_standard_commands.c"
  21. #include "templates_skin.c"
  22. #include "templates_template.c"
  23. static struct template_skin *skins;
  24. /* init_templates()
  25. * initializes some global variables
  26. */
  27. static void init_templates()
  28. {
  29. skins = NULL;
  30. glob_tpl_cmd_list = NULL;
  31. glob_tpl_cmd_list = templates_commands_list_add(glob_tpl_cmd_list, templates_standard_commands);
  32. }
  33. /* unload_templates()
  34. * removes every template-related stuff from memory
  35. */
  36. static void unload_templates()
  37. {
  38. Context;
  39. templates_skin_free(skins);
  40. templates_commands_list_free(glob_tpl_cmd_list);
  41. skins = NULL;
  42. Context;
  43. }
  44. /* expmem_templates():
  45. * returns the memory usage of the template-system
  46. */
  47. /*
  48. static int expmem_templates()
  49. {
  50. int size = 0;
  51. Context;
  52. size += templates_skin_expmem(skins);
  53. size += templates_commands_list_expmem(glob_tpl_cmd_list);
  54. Context;
  55. return size;
  56. }
  57. */
  58. static int loadskin(char *parbuf)
  59. {
  60. FILE *f;
  61. char *buf, *s, *cmd, *str_skin, *name, *filename, *shortname, *longname;
  62. char *conffile, *path, *filebuf;
  63. struct template_skin *skin;
  64. struct template_content *content;
  65. struct slang_header *slang;
  66. int len;
  67. f = fopen(parbuf, "r");
  68. if (!f) {
  69. putlog(LOG_MISC, "*", "ERROR loading skin! Couldn't open config "
  70. "file '%s'!", parbuf);
  71. return 0;
  72. }
  73. debug1("parbuf: '%s'", parbuf);
  74. conffile = inverted_csplit(&parbuf, '/');
  75. path = parbuf;
  76. debug2("path: '%s', conffile: '%s'", path, conffile);
  77. if (!path[0]) {
  78. path = ".";
  79. debug1("empty path, new path: %s", path);
  80. }
  81. skin = NULL;
  82. buf = nmalloc(2000);
  83. while (!feof(f)) {
  84. s = buf;
  85. if (fgets(s, 2000, f)) {
  86. // at first, kill those stupid line feeds and carriage returns...
  87. if (s[strlen(s) - 1] == '\n')
  88. s[strlen(s) - 1] = 0;
  89. if (s[strlen(s) - 1] == '\r')
  90. s[strlen(s) - 1] = 0;
  91. if (!s[0])
  92. continue;
  93. cmd = newsplit(&s);
  94. if (!strcasecmp(cmd, "skin")) {
  95. str_skin = newsplit(&s);
  96. debug2("adding skin '%s' (%s)", str_skin, s);
  97. skins = templates_skin_add(skins, str_skin, s);
  98. skin = templates_skin_find(skins, str_skin);
  99. if (!skin) {
  100. putlog(LOG_MISC, "*", "ERROR loading skin: unknown error creating skin structure!");
  101. fclose(f);
  102. nfree(buf);
  103. return 0;
  104. }
  105. } else if (!strcasecmp(cmd, "template")) {
  106. name = newsplit(&s);
  107. filename = newsplit(&s);
  108. if (!name[0] || !filename[0]) {
  109. putlog(LOG_MISC, "*", "ERROR loading template: Too few parameters!");
  110. continue;
  111. }
  112. len = strlen(path) + 1 + strlen(filename) + 1;
  113. filebuf = nmalloc(len);
  114. snprintf(filebuf, len, "%s/%s", path, filename);
  115. putlog(LOG_MISC, "*", "Loading template '%s' from '%s'...", name, filebuf);
  116. content = templates_content_load(filebuf);
  117. nfree(filebuf);
  118. if (!content) {
  119. putlog(LOG_MISC, "*", "ERROR loading template from '%s'!", filename);
  120. continue;
  121. }
  122. skin->templates = templates_template_add_parsedcontent(skin->templates,
  123. name, content);
  124. } else if (!strcasecmp(cmd, "slang")) {
  125. filename = newsplit(&s);
  126. shortname = newsplit(&s);
  127. longname = s;
  128. if (!shortname[0] || !longname[0] || !filename[0]) {
  129. putlog(LOG_MISC, "*", "ERROR loading slang for skin '%s': too few "
  130. "parameters!", skin->name);
  131. continue;
  132. }
  133. skin->slang = slang_create(skin->slang, shortname, longname);
  134. slang = slang_find(skin->slang, shortname);
  135. len = strlen(path) + 1 + strlen(filename) + 1;
  136. filebuf = nmalloc(len);
  137. snprintf(filebuf, len, "%s/%s", path, filename);
  138. if (!slang_load(slang, filebuf)) {
  139. putlog(LOG_MISC, "*", "ERROR loading slang for skin '%s'",
  140. skin->name);
  141. nfree(filebuf);
  142. continue;
  143. }
  144. nfree(filebuf);
  145. }
  146. }
  147. }
  148. nfree(buf);
  149. return 1;
  150. }
  151. static void template_send(struct template_skin *skin, char *name, int idx)
  152. {
  153. struct templates_template *tpl;
  154. Assert(skin);
  155. tpl = templates_template_find(skin->templates, name);
  156. if (!tpl) {
  157. dprintf(idx, "<H1>Template not found: %s</H1>", name);
  158. return;
  159. }
  160. templates_content_send(tpl->contents, idx);
  161. }