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
|
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <tcl.h>
#include <tk.h>
#include "gis.h"
#include "dbmi.h"
#include "form.h"
#ifdef USE_G_SOCKS
#include <sys/types.h>
#include <sys/socket.h>
#endif /*USE_G_SOCKS*/
int first = 1;
/* the pipe to send data to GUI */
FILE *parent_send, *parent_recv;
#ifdef USE_G_SOCKS
int pipefd[2];
#define pfd pipefd[1] /* parent's end */
#define cfd pipefd[0] /* child's end */
#endif /*USE_G_SOCKS*/
/* Open new form
*
* returns: 0 success
*/
int
F_open ( char *title, char *html )
{
/* parent */
int c;
/* common */
static int pid;
#ifndef USE_G_SOCKS
static int p1[2], p2[2];
#endif /*USE_G_SOCKS*/
int length;
/* child */
char buf[2000];
G_debug ( 2, "F_open(): title = %s", title);
if ( first ) {
#ifdef USE_G_SOCKS
if ( G_sock_socketpair(AF_UNIX, SOCK_STREAM, 0, pipefd) < 0)
G_fatal_error ("Cannot make socket pair");
#else
if ( pipe(p1) < 0 || pipe(p2) < 0 )
G_fatal_error ("Cannot open pipe");
#endif /*USE_G_SOCKS*/
if ((pid = fork()) < 0) G_fatal_error ("Cannot create fork");
}
if ( pid == 0 ) { /* Child */
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 USE_G_SOCKS
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 /*USE_G_SOCKS*/
sprintf(buf,"%s/etc/form/form", G_gisbase());
execl ("/bin/sh", "sh", "-c", buf, 0);
G_debug(2, "CHILD END\n");
exit (0);
} else { /* Parent */
G_debug ( 2, "PARENT" );
if ( first ) {
#ifndef USE_G_SOCKS
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 /*USE_G_SOCKS*/
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;
}
/* 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;
}
|