kcms.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include <sys/mman.h>
  2. #include <sys/stat.h>
  3. #include <err.h>
  4. #include <fcntl.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include "buffer.h"
  9. #include "filehelper.h"
  10. #include "template.h"
  11. #ifndef CMS_CONTENT_DIR
  12. #error "Need CMS_CONTENT_DIR defined to compile"
  13. #endif
  14. #ifndef CMS_HOSTNAME
  15. #error "Need CMS_HOSTNAME defined to compile"
  16. #endif
  17. static __dead void usage(void);
  18. __dead void
  19. usage(void)
  20. {
  21. extern char *__progname;
  22. fprintf(stderr, "usage: %s -f file\n", __progname);
  23. exit(1);
  24. }
  25. int
  26. main(int argc, char **argv)
  27. {
  28. int ch, fd = -1;
  29. while ((ch = getopt(argc, argv, "f:h")) != -1) {
  30. switch (ch) {
  31. case 'f':
  32. if ((fd = open(optarg, O_RDONLY, 0)) == -1)
  33. err(1, "%s", optarg);
  34. break;
  35. default:
  36. usage();
  37. }
  38. }
  39. argc -= optind;
  40. argv += optind;
  41. struct tmpl_data *data = tmpl_data_new();
  42. if (-1 == fd)
  43. usage();
  44. struct stat sb;
  45. if (-1 == fstat(fd, &sb))
  46. err(1, NULL);
  47. void *tmpl = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, fd, 0);
  48. if (NULL == tmpl)
  49. err(1, NULL);
  50. tmpl_data_set_variable(data, "LANGUAGE", "de");
  51. tmpl_data_set_variable(data, "TITLE", "Template-Test");
  52. struct tmpl_loop *language_links = tmpl_data_add_loop(
  53. data, "LANGUAGE_LINKS"
  54. );
  55. struct tmpl_data *d = tmpl_data_new();
  56. tmpl_data_set_variable(d, "LANGUAGE_LINK",
  57. "<a href=\"de-page.html\">de</a>");
  58. tmpl_loop_add_data(language_links, d);
  59. d = tmpl_data_new();
  60. tmpl_data_set_variable(d, "LANGUAGE_LINK",
  61. "<a href=\"en-page.html\">en</a>");
  62. tmpl_loop_add_data(language_links, d);
  63. struct buffer_list *out = tmpl_parse((char *)tmpl, sb.st_size, data);
  64. tmpl_data_free(data);
  65. close(fd);
  66. char *result = buffer_list_concat_string(out);
  67. printf("%s", result);
  68. return 0;
  69. }