File: open.c

package info (click to toggle)
grass 6.4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 104,028 kB
  • ctags: 40,409
  • sloc: ansic: 419,980; python: 63,559; tcl: 46,692; cpp: 29,791; sh: 18,564; makefile: 7,000; xml: 3,505; yacc: 561; perl: 559; lex: 480; sed: 70; objc: 7
file content (203 lines) | stat: -rw-r--r-- 3,732 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <grass/gis.h>
#include <grass/dbmi.h>
#include <grass/form.h>

#ifdef HAVE_SOCKET
#include <sys/types.h>
#ifdef __MINGW32__
#include <winsock.h>

#else /*__MINGW32__*/
#include <sys/socket.h>
#include <netinet/in.h>

#endif /*__MINGW32__*/
#endif /*HAVE_SOCKET */

#ifdef HAVE_SOCKET
static int make_socketpair(int *);
#endif

int first = 1;

/* the pipe to send data to GUI */
FILE *parent_send, *parent_recv;

#ifdef HAVE_SOCKET
int pipefd[2];

#define	pfd	pipefd[1]	/* parent's end */
#define	cfd	pipefd[0]	/* child's end */

#endif /*HAVE_SOCKET */

/* Open new form
 *
 *  returns: 0 success
 */
#ifdef __MINGW32__
int F_open(char *title, char *html)
{
    G_fatal_error("F_open is not supported on Windows");
    return 1;
}
#else
int F_open(char *title, char *html)
{
    /* parent */
    int c;

    /* common */
    static int pid;

#ifndef HAVE_SOCKET
    static int p1[2], p2[2];
#endif /*HAVE_SOCKET */
    int length;

    /* child */

    G_debug(2, "F_open(): title = %s", title);

    if (first) {
#ifdef HAVE_SOCKET
	if (make_socketpair(pipefd) < 0)
	    G_fatal_error("Cannot make socket pair");
#else
	if (pipe(p1) < 0 || pipe(p2) < 0)
	    G_fatal_error("Cannot open pipe");
#endif /*HAVE_SOCKET */

	if ((pid = fork()) < 0)
	    G_fatal_error("Cannot create fork");
    }

    if (pid == 0) {		/* Child */
	char command[2000], script[2000];

	G_debug(2, "CHILD");

	/* Note: If you are forking in a Tk based apllication  you
	 *       must  execl  before  doing any window operations in the
	 *       child or you will receive an error from the  X server */

	close(0);
	close(1);

#ifndef HAVE_SOCKET
	close(p1[1]);
	close(p2[0]);
	if (dup(p1[0]) != 0)
	    G_fatal_error("Form: cannot dup() input");
	if (dup(p2[1]) != 1)
	    G_fatal_error("Form: cannot dup() output");

#else
	close(pfd);
	if (dup(cfd) != 0)
	    G_fatal_error("Form: cannot dup() input");
	if (dup(cfd) != 1)
	    G_fatal_error("Form: cannot dup() output");

#endif /*HAVE_SOCKET */



	sprintf(command, "%s/etc/form/form", G_gisbase());
	sprintf(script, "%s/etc/form/form.tcl", G_gisbase());

	execl(command, "form", "-f", script, NULL);

	G_debug(2, "CHILD END\n");
	exit(0);

    }
    else {			/* Parent */
	G_debug(2, "PARENT");

	if (first) {
#ifndef HAVE_SOCKET
	    parent_send = fdopen(p1[1], "w");
	    close(p1[0]);
	    parent_recv = fdopen(p2[0], "r");
	    close(p2[1]);
#else
	    close(cfd);
	    parent_send = fdopen(pfd, "w");
	    parent_recv = fdopen(pfd, "r");
#endif /*HAVE_SOCKET */
	    first = 0;
	}

	G_debug(2, "PARENT HTML:\n%s\n", html);

	fprintf(parent_send, "O");
	length = strlen(title);
	fprintf(parent_send, "%d\n", length);
	fprintf(parent_send, "%s", title);
	length = strlen(html);
	fprintf(parent_send, "%d\n", length);
	fprintf(parent_send, "%s", html);
	fflush(parent_send);
	G_debug(2, "PARENT: Request sent\n");

	/* Wait for response */
	c = fgetc(parent_recv);
	G_debug(2, "PARENT: received %c\n", c);
    }

    return 0;
}
#endif

/* Clear old forms from window
 *
 */
void F_clear(void)
{
    char c;

    G_debug(2, "F_clear()");

    if (first)
	return;

    fprintf(parent_send, "C");
    fflush(parent_send);
    c = fgetc(parent_recv);
    G_debug(2, "PARENT: received %c\n", c);
}

void F_close(void)
{
    char c;

    G_debug(2, "F_close()");

    if (first)
	return;

    fprintf(parent_send, "D");
    fflush(parent_send);
    c = fgetc(parent_recv);
    G_debug(2, "PARENT: received %c\n", c);

    first = 1;
}

#ifdef HAVE_SOCKET
static int make_socketpair(int *fd)
{
    int n;

    if ((n = socketpair(AF_UNIX, SOCK_STREAM, IPPROTO_IP, fd)) < 0)
	return -1;
    else
	return 0;
}

#endif