FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Windows stdin workaround. · gnuplot/gnuplot@cb49ad3 · GitHub

Repository navigation

Commit cb49ad3

Browse files
Lars Hecking
committed
Windows stdin workaround.
1 parent a5eab69 commit cb49ad3

1 file changed

Lines changed: 141 additions & 0 deletions

File tree

‎win/pgnuplot.c‎

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/*
2+
* pgnuplot.c -- 'Pipe to gnuplot' Version 990211
3+
*
4+
* A small program, to be compiled as a Win32 console mode application.
5+
* (NB: will not work on 16bit Windows, not even with Win32s installed).
6+
*
7+
* This program will accept commands on STDIN, and pipe them to an
8+
* active (or newly created) wgnuplot text window. Command line options
9+
* are passed on to wgnuplot.
10+
*
11+
* Effectively, this means `pgnuplot' is an almost complete substitute
12+
* for `wgnuplot', on the command line, with the added benefit that it
13+
* does accept commands from redirected stdin. (Being a Windows GUI
14+
* application, `wgnuplot' itself cannot read stdin at all.)
15+
*
16+
* Copyright (C) 1999 by Hans-Bernhard Broeker
17+
* (broeker@physik.rwth-aachen.de)
18+
* This file is in the public domain. It might become part of a future
19+
* distribution of gnuplot.
20+
*
21+
* based on a program posted to comp.graphics.apps.gnuplot in May 1998 by
22+
* jl Hamel <jlhamel@cea.fr>
23+
*
24+
* Changes relative to that original version:
25+
* -- doesn't start a new wgnuplot if one already is running.
26+
* -- doesn't end up in an endless loop if STDIN is not redirected.
27+
* (refuses to read from STDIN at all, in that case).
28+
* -- doesn't stop the wgnuplot session at the end of
29+
* stdin, if it didn't start wgnuplot itself.
30+
* -- avoids the usual buffer overrun problem with gets().
31+
*
32+
* For the record, I usually use MinGW32 to compile this, with a
33+
* command line looking like this:
34+
*
35+
* gcc -o pgnuplot.exe pgnuplot.c -luser32 -s
36+
*/
37+
38+
#include <io.h>
39+
#include <stdio.h>
40+
#include <string.h>
41+
#include <stdlib.h>
42+
#include <windows.h>
43+
44+
#define FULLPATH "e:/prg/gp36/gp37hbb/wgnuplot.exe"
45+
#define WINDOWNAME "gnuplot"
46+
#define PARENTCLASS "wgnuplot_parent"
47+
#define TEXTCLASS "wgnuplot_text"
48+
#define GRAPHWINDOW "gnuplot graph"
49+
#define GRAPHCLASS "wgnuplot_graph"
50+
51+
int main (int argc, char *argv[])
52+
{
53+
/* Customize this path if needed */
54+
char *d, buf[80];
55+
HWND hwnd_parent;
56+
HWND hwnd_text;
57+
BOOL startedWgnuplotMyself = FALSE;
58+
59+
/* First, try to find if there is an instance of gnuplot
60+
* running, already. If so, use that. */
61+
hwnd_parent = FindWindow(PARENTCLASS, WINDOWNAME);
62+
63+
if ( ! hwnd_parent) {
64+
/* None there, so start one: load gnuplot (minimized in order to
65+
* show only the graphic window). Pass all command line arguments
66+
* on to wgnuplot, by concatting the wgnuplot full path name and
67+
* the given arguments, building up a new, usable command line:
68+
*/
69+
char *cmdline = strdup (FULLPATH);
70+
71+
while (*(++argv)) {
72+
/* Puzzle together a working from the given arguments. To account
73+
* for possible spaces in arguments, we'll have to put double quotes
74+
* around each of them:
75+
*/
76+
/* FIXME: doesn't check for out of memory */
77+
cmdline = realloc(cmdline, strlen(cmdline)+3+strlen(argv[0]));
78+
strcat(cmdline, " \"");
79+
strcat(cmdline, *argv);
80+
strcat(cmdline, "\"");
81+
}
82+
83+
if (WinExec(cmdline, SW_SHOWMINNOACTIVE) < 32) {
84+
printf("Can't load gnuplot\n");
85+
exit(EXIT_FAILURE);
86+
}
87+
88+
startedWgnuplotMyself = TRUE;
89+
90+
/* wait for the gnuplot window */
91+
/* FIXME: is this necessary? As documented, WinExec shouldn't return
92+
* until wgnuplot first calls GetMessage(). By then, the window should
93+
* be there, shouldn't it?
94+
*/
95+
Sleep(1000);
96+
hwnd_parent = FindWindow(PARENTCLASS, WINDOWNAME);
97+
}
98+
99+
if ( ! hwnd_parent) {
100+
/* Still no gnuplot window? Problem! */
101+
printf("Can't find the gnuplot window");
102+
exit(EXIT_FAILURE);
103+
}
104+
105+
/* find the child text window */
106+
hwnd_text = FindWindowEx(hwnd_parent, NULL, "wgnuplot_text", NULL );
107+
108+
if (isatty(fileno(stdin))) {
109+
/* Do not try to read from stdin if it hasn't been redirected
110+
* (i.e., it should read from a pipe or a file) */
111+
exit(EXIT_SUCCESS);
112+
}
113+
114+
/* wait for commands on stdin, and pass them on to the wgnuplot text
115+
* window */
116+
do {
117+
d = fgets(buf, sizeof(buf), stdin);
118+
119+
if (NULL == d) {
120+
if (startedWgnuplotMyself) {
121+
/* close gnuplot cleanly: */
122+
strcpy (buf, "exit");
123+
d = buf;
124+
startedWgnuplotMyself = FALSE;
125+
} else {
126+
break;
127+
}
128+
}
129+
130+
while(*d) {
131+
PostMessage(hwnd_text, WM_CHAR, *d, 1L);
132+
d++;
133+
}
134+
} while (NULL != d);
135+
136+
/* Just in case stdin didn't have a terminating newline, add one: */
137+
PostMessage(hwnd_text, WM_CHAR, '\n', 1L);
138+
139+
return EXIT_SUCCESS;
140+
}
141+

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL