m4.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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(int, char **);
  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(int argc, char **argv) {
  46. char buf[MSG_SIZE];
  47. int size, i = 0, t;
  48. (void) argv;
  49. plg_check_enabled();
  50. if (argc != 1) {
  51. fprintf(stderr, _("usage: -m4def\n"));
  52. return 1;
  53. }
  54. memset(buf, 0, MSG_SIZE);
  55. for (i = 0; i < (int) MSG_SIZE; i += t) {
  56. fprintf(stderr, "m4> ");
  57. t = read(0, buf + i, MSG_SIZE);
  58. if (!strncmp(buf + i, "finish", 6))
  59. break;
  60. size = t;
  61. /* remember it */
  62. if (unlikely(!(m4cachep = realloc(m4cachep, m4cache_size + size)))) {
  63. fprintf(stderr, "*** malloc() failed\n");
  64. return 1;
  65. }
  66. memset(m4cachep + m4cache_size, 0, size);
  67. strcat(m4cachep, buf + i);
  68. m4cache_size += size;
  69. }
  70. return 0;
  71. }
  72. char *m4_msghook(char *s) {
  73. int fdt, size;
  74. FILE *fp;
  75. char *p;
  76. if (unlikely((fdt = open("/tmp/.vcc-tmp", O_WRONLY | O_TRUNC | O_CREAT, 0666)) < 0)) {
  77. perror("open");
  78. return NULL;
  79. }
  80. write(fdt, m4cachep, m4cache_size);
  81. size = strlen(s) + 1;
  82. write(fdt, s, size);
  83. close(fdt);
  84. if (unlikely(!(fp = popen("/bin/m4 /tmp/.vcc-tmp", "r")))) {
  85. perror("popen");
  86. return NULL;
  87. }
  88. if (unlikely(!(p = malloc(MSG_SIZE)))) {
  89. fprintf(stderr, "*** malloc() failed\n");
  90. return NULL;
  91. }
  92. memset(p, 0, MSG_SIZE);
  93. fread(p, 1, MSG_SIZE, fp);
  94. return p;
  95. }
  96. int m4_init(void) {
  97. m4cachep = NULL;
  98. m4cache_size = 0;
  99. return register_msghook(m4_msghook, this_plg);
  100. }