m4.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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/version.h>
  28. int m4_init(void);
  29. int do_cmd_m4def(char *s);
  30. char *m4cachep;
  31. int m4cache_size;
  32. struct cmdtable m4_cmdtab[] = {
  33. { "-m4def", do_cmd_m4def, "Define a macro which i will remember. " },
  34. { NULL, NULL, NULL }
  35. };
  36. struct plugin plg_m4 = {
  37. .state = 0,
  38. .name = "m4",
  39. .descr = "GNU M4 Expanding. ",
  40. .init = m4_init,
  41. /* the hook will remove by plugins.c */
  42. .cmdtab = m4_cmdtab
  43. };
  44. #define this_plg (&plg_m4)
  45. int do_cmd_m4def(char *s) {
  46. char buf[MSG_SIZE];
  47. int size, i = 0, t;
  48. (void) s;
  49. plg_check_enabled();
  50. memset(buf, 0, MSG_SIZE);
  51. for (i = 0; i < (int) MSG_SIZE; i += t) {
  52. fprintf(stderr, "m4> ");
  53. t = read(0, buf + i, MSG_SIZE);
  54. if (!strncmp(buf + i, "finish", 6))
  55. break;
  56. size = t;
  57. /* remember it */
  58. if (unlikely(!(m4cachep = realloc(m4cachep, m4cache_size + size)))) {
  59. fprintf(stderr, "*** malloc() failed\n");
  60. return 1;
  61. }
  62. memset(m4cachep + m4cache_size, 0, size);
  63. strcat(m4cachep, buf + i);
  64. m4cache_size += size;
  65. }
  66. printf("%s\n", m4cachep);
  67. return 0;
  68. }
  69. char *m4_msghook(char *s) {
  70. int fdt, size;
  71. FILE *fp;
  72. char *p;
  73. if (unlikely((fdt = open("/tmp/.vcc-tmp", O_WRONLY | O_TRUNC | O_CREAT, 0666)) < 0)) {
  74. perror("open");
  75. return NULL;
  76. }
  77. write(fdt, m4cachep, m4cache_size);
  78. size = strlen(s) + 1;
  79. write(fdt, s, size);
  80. close(fdt);
  81. if (unlikely(!(fp = popen("/bin/m4 /tmp/.vcc-tmp", "r")))) {
  82. perror("popen");
  83. return NULL;
  84. }
  85. if (unlikely(!(p = malloc(size)))) {
  86. fprintf(stderr, "*** malloc() failed\n");
  87. return NULL;
  88. }
  89. memset(p, 0, size);
  90. fread(p, 1, size << 2, fp);
  91. return p;
  92. }
  93. int m4_init(void) {
  94. m4cachep = NULL;
  95. m4cache_size = 0;
  96. return register_hook(m4_msghook, this_plg);
  97. }