m4.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. int m4_write_predef(int fd) {
  79. struct history *hist;
  80. if (!check_hist_enabled()) {
  81. fprintf(stderr, _("the history plugin (hist) doesn't insert so m4 preder can't work. \n"));
  82. return 0;
  83. }
  84. hist = hist_getmsg(1);
  85. if (!hist)
  86. return 0;
  87. dprintf(fd, "divert(-1)define(`LAST_MSG', `%s'\n)divert(0)dnl\n", hist->msg);
  88. return 0;
  89. }
  90. char *m4_hook(struct hook_info *hi) {
  91. int fdt, size;
  92. FILE *fp;
  93. char *p;
  94. if (unlikely((fdt = open("/tmp/.vcc-tmp", O_WRONLY | O_TRUNC | O_CREAT, 0666)) < 0)) {
  95. perror("open");
  96. return NULL;
  97. }
  98. write(fdt, m4cachep, m4cache_size);
  99. m4_write_predef(fdt);
  100. size = strlen(hi->msg) + 1;
  101. write(fdt, hi->msg, size);
  102. close(fdt);
  103. if (unlikely(!(fp = popen("/bin/m4 /tmp/.vcc-tmp", "r")))) {
  104. perror("popen");
  105. return NULL;
  106. }
  107. if (unlikely(!(p = malloc(MSG_SIZE)))) {
  108. fprintf(stderr, "*** malloc() failed\n");
  109. return NULL;
  110. }
  111. memset(p, 0, MSG_SIZE);
  112. fread(p, 1, MSG_SIZE, fp);
  113. return p;
  114. }
  115. int m4_init(void) {
  116. m4cachep = NULL;
  117. m4cache_size = 0;
  118. register_hook(HOOK_SEND, m4_hook);
  119. return 0;
  120. }