pgnuplot.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include <windows.h>
  2. /* You MUST change the value of FULLPATH to the actual location of the
  3. * gnuplot executable on your computer. */
  4. #define FULLPATH "e:/prg/gp36/gp37hbb/wgnuplot.exe"
  5. #define WINDOWNAME "gnuplot"
  6. #define PARENTCLASS "wgnuplot_parent"
  7. #define TEXTCLASS "wgnuplot_text"
  8. #define GRAPHWINDOW "gnuplot graph"
  9. #define GRAPHCLASS "wgnuplot_graph"
  10. int main (int argc, char *argv[])
  11. {
  12. /* Customize this path if needed */
  13. char *d, buf[80];
  14. HWND hwnd_parent;
  15. HWND hwnd_text;
  16. BOOL startedWgnuplotMyself = FALSE;
  17. /* First, try to find if there is an instance of gnuplot
  18. * running, already. If so, use that. */
  19. hwnd_parent = FindWindow(PARENTCLASS, WINDOWNAME);
  20. if ( ! hwnd_parent) {
  21. /* None there, so start one: load gnuplot (minimized in order to
  22. * show only the graphic window). Pass all command line arguments
  23. * on to wgnuplot, by concatting the wgnuplot full path name and
  24. * the given arguments, building up a new, usable command line:
  25. */
  26. char *cmdline = strdup (FULLPATH);
  27. while (*(++argv)) {
  28. /* Puzzle together a working from the given arguments. To account
  29. * for possible spaces in arguments, we'll have to put double quotes
  30. * around each of them:
  31. */
  32. /* FIXME: doesn't check for out of memory */
  33. cmdline = realloc(cmdline, strlen(cmdline)+3+strlen(argv[0]));
  34. strcat(cmdline, " \"");
  35. strcat(cmdline, *argv);
  36. strcat(cmdline, "\"");
  37. }
  38. if (WinExec(cmdline, SW_SHOWMINNOACTIVE) < 32) {
  39. printf("Can't load gnuplot\n");
  40. exit(EXIT_FAILURE);
  41. }
  42. startedWgnuplotMyself = TRUE;
  43. /* wait for the gnuplot window */
  44. /* FIXME: is this necessary? As documented, WinExec shouldn't return
  45. * until wgnuplot first calls GetMessage(). By then, the window should
  46. * be there, shouldn't it?
  47. */
  48. Sleep(1000);
  49. hwnd_parent = FindWindow(PARENTCLASS, WINDOWNAME);
  50. }
  51. if ( ! hwnd_parent) {
  52. /* Still no gnuplot window? Problem! */
  53. printf("Can't find the gnuplot window");
  54. exit(EXIT_FAILURE);
  55. }
  56. /* find the child text window */
  57. hwnd_text = FindWindowEx(hwnd_parent, NULL, "wgnuplot_text", NULL );
  58. if (isatty(fileno(stdin))) {
  59. /* Do not try to read from stdin if it hasn't been redirected
  60. * (i.e., it should read from a pipe or a file) */
  61. exit(EXIT_SUCCESS);
  62. }
  63. /* wait for commands on stdin, and pass them on to the wgnuplot text
  64. * window */
  65. do {
  66. d = fgets(buf, sizeof(buf), stdin);
  67. if (NULL == d) {
  68. if (startedWgnuplotMyself) {
  69. /* close gnuplot cleanly: */
  70. strcpy (buf, "exit");
  71. d = buf;
  72. startedWgnuplotMyself = FALSE;
  73. } else {
  74. break;
  75. }
  76. }
  77. while(*d) {
  78. PostMessage(hwnd_text, WM_CHAR, *d, 1L);
  79. d++;
  80. }
  81. } while (NULL != d);
  82. /* Just in case stdin didn't have a terminating newline, add one: */
  83. PostMessage(hwnd_text, WM_CHAR, '\n', 1L);
  84. return EXIT_SUCCESS;
  85. }