gml4gtk-plugin.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* dot plugin internal common routines.
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 2 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  12. #include "gml4gtk-plugin.h"
  13. /* Used as a string buffer. */
  14. static struct obstack str_obstack;
  15. /* Print error messages. */
  16. static void
  17. dot_error (const char *format, ...)
  18. {
  19. va_list ap;
  20. va_start (ap, format);
  21. fprintf (stderr, "%s: error: ", dot_plugin_common.plugin_name);
  22. vfprintf (stderr, format, ap);
  23. va_end (ap);
  24. fputc ('\n', stderr);
  25. return;
  26. }
  27. /* Dump the top graph into file FNAME. */
  28. static void
  29. dot_dump (char *fname)
  30. {
  31. FILE *fp;
  32. if ((fp = fopen (fname, "w")) == NULL)
  33. {
  34. dot_plugin_common.error ("failed to open file %s.", fname);
  35. return;
  36. }
  37. /* Put the version information on top. */
  38. fprintf (fp, "// Generated by GCC gml4gtk Plugin %s\n" \
  39. "// %s", dot_plugin_common.version, dot_plugin_common.info);
  40. g_dump_graph (fp, dot_plugin_common.top_graph);
  41. fclose (fp);
  42. return;
  43. }
  44. /* Show the top graph. */
  45. static void
  46. dot_show (char *fname)
  47. {
  48. char *cmd;
  49. pid_t pid;
  50. cmd = concat (dot_plugin_common.dot_viewer, " ", fname, NULL);
  51. pid = fork ();
  52. if (pid == 0)
  53. {
  54. system (cmd);
  55. exit (0);
  56. }
  57. free (cmd);
  58. return;
  59. }
  60. /* Do the common initialization work for each view/dump command. */
  61. static void
  62. dot_init (void)
  63. {
  64. g_graph *graph;
  65. /* Create the top graph. */
  66. graph = g_new_graph ("top graph");
  67. dot_plugin_common.top_graph = graph;
  68. /* Initialize the string obstack. */
  69. gcc_obstack_init (&str_obstack);
  70. /* Open the temp stream. */
  71. dot_plugin_common.stream = open_memstream (&dot_plugin_common.stream_buf,
  72. &dot_plugin_common.stream_buf_size);
  73. return;
  74. }
  75. /* Do the common cleanup work for each view/dump command. */
  76. static void
  77. dot_finish (void)
  78. {
  79. g_free_graph (dot_plugin_common.top_graph);
  80. obstack_free (&str_obstack, NULL);
  81. fclose (dot_plugin_common.stream);
  82. free (dot_plugin_common.stream_buf);
  83. return;
  84. }
  85. /* Print into the string buffer. */
  86. static void
  87. dot_buf_print (char *fmt, ...)
  88. {
  89. va_list ap;
  90. va_start (ap, fmt);
  91. obstack_vprintf (&str_obstack, fmt, ap);
  92. va_end (ap);
  93. return;
  94. }
  95. /* Finish a string and return the address. */
  96. static void *
  97. dot_buf_finish (void)
  98. {
  99. obstack_1grow (&str_obstack, '\0');
  100. return obstack_finish (&str_obstack);
  101. }
  102. dot_plugin_common_t dot_plugin_common =
  103. {
  104. /* The plugin name. */
  105. "Gml4gtk Plugin",
  106. /* The plugin version. */
  107. "2.8",
  108. /* The gcc base version. */
  109. NULL,
  110. /* Other information. */
  111. NULL,
  112. /* The name of the viewer tool. */
  113. "gml4gtk",
  114. /* The top graph. */
  115. NULL,
  116. /* Temp file name to dump/view a graph. */
  117. "dump-temp.dot",
  118. /* Temp stream to get gcc dump. */
  119. NULL,
  120. NULL,
  121. 0,
  122. dot_init,
  123. dot_finish,
  124. dot_error,
  125. dot_dump,
  126. dot_show,
  127. dot_buf_print,
  128. dot_buf_finish
  129. };
  130. /* end */