m4.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* vcc/plugins/m4/m4.c
  2. *
  3. * This file is part of vcc.
  4. *
  5. * vcc is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * vcc is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with vcc. If not, see <https://www.gnu.org/licenses/>
  17. */
  18. #include <stdio.h>
  19. #include <unistd.h>
  20. #include <fcntl.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <sys/wait.h>
  24. #include <vcc/vcc.h>
  25. #include <vcc/pretty.h>
  26. #include <vcc/plugin.h>
  27. #include <vcc/interfaces.h>
  28. #include <vcc/version.h>
  29. int m4_init(void);
  30. int do_cmd_m4def(int, char **);
  31. char *m4cachep;
  32. int m4cache_size;
  33. struct cmdtable m4_cmdtab[] = {
  34. { "-m4def", do_cmd_m4def, "Define a macro which i will remember. " },
  35. { NULL, NULL, NULL }
  36. };
  37. struct plugin plg_m4 = {
  38. .state = 0,
  39. .name = "m4",
  40. .descr = "GNU M4 Expanding. ",
  41. .init = m4_init,
  42. /* the hook will remove by plugins.c */
  43. .cmdtab = m4_cmdtab
  44. };
  45. #define this_plg (&plg_m4)
  46. int do_cmd_m4def(int argc, char **argv) {
  47. char buf[MSG_SIZE];
  48. int size, i = 0, t;
  49. (void) argv;
  50. plg_check_enabled();
  51. if (argc != 1) {
  52. fprintf(stderr, _("usage: -m4def\n"));
  53. return 1;
  54. }
  55. memset(buf, 0, MSG_SIZE);
  56. for (i = 0; i < (int) MSG_SIZE; i += t) {
  57. fprintf(stderr, "m4> ");
  58. t = read(0, buf + i, MSG_SIZE);
  59. if (!strncmp(buf + i, "finish", 6))
  60. break;
  61. size = t;
  62. /* remember it */
  63. if (unlikely(!(m4cachep = realloc(m4cachep, m4cache_size + size)))) {
  64. fprintf(stderr, "*** malloc() failed\n");
  65. return 1;
  66. }
  67. memset(m4cachep + m4cache_size, 0, size);
  68. strcat(m4cachep, buf + i);
  69. m4cache_size += size;
  70. }
  71. return 0;
  72. }
  73. static inline int check_hist_enabled(void) {
  74. if (find_plg("hist")->state == PLG_INSERTED)
  75. return 1;
  76. return 0;
  77. }
  78. /* this causes a infinite recursion, which is the thing m4 programmers are afraid of. */
  79. int m4_write_predef(char *buf) {
  80. (void) buf;
  81. #if 0
  82. struct history *hist;
  83. char *p;
  84. char s[128];
  85. if (!check_hist_enabled()) {
  86. fprintf(stderr, _("the history plugin (hist) doesn't insert so m4 preder can't work. \n"));
  87. return 0;
  88. }
  89. hist = hist_getmsg(1);
  90. if (!hist)
  91. p = "\n";
  92. else
  93. p = hist->msg;
  94. sprintf(s, "divert(-1)define(`LAST_MSG', `%s'\n)divert(0)dnl\n", p);
  95. strcat(buf, s);
  96. #endif
  97. return 0;
  98. }
  99. char *m4_hook(struct hook_info *hi) {
  100. int fdt;
  101. FILE *fp;
  102. char *p;
  103. if (unlikely((fdt = open("/tmp/.vcc-tmp", O_WRONLY | O_TRUNC | O_CREAT | O_APPEND, 0666)) < 0)) {
  104. perror("open");
  105. return NULL;
  106. }
  107. if (unlikely(!(p = malloc(8192)))) {
  108. fprintf(stderr, "*** malloc() failed\n");
  109. return NULL;
  110. }
  111. memset(p, 0, 8192);
  112. if (m4cachep)
  113. strcat(p, m4cachep);
  114. m4_write_predef(p);
  115. strcat(p, hi->msg);
  116. write(fdt, p, strlen(p));
  117. close(fdt);
  118. if (unlikely(!(fp = popen("/bin/m4 /tmp/.vcc-tmp", "r")))) {
  119. perror("popen");
  120. return NULL;
  121. }
  122. if (unlikely(!(p = malloc(MSG_SIZE)))) {
  123. fprintf(stderr, "*** malloc() failed\n");
  124. return NULL;
  125. }
  126. memset(p, 0, MSG_SIZE);
  127. fread(p, 1, MSG_SIZE, fp);
  128. return p;
  129. }
  130. int m4_init(void) {
  131. m4cachep = NULL;
  132. m4cache_size = 0;
  133. #if 0
  134. if (check_hist_enabled()) {
  135. fprintf(stderr, "*** fatal: enable m4 after the hist enabled will cause a RECURSION when try to use LAST_MSG! \n");
  136. return 1;
  137. }
  138. #endif
  139. register_hook(HOOK_SEND, m4_hook);
  140. return 0;
  141. }