1 #include <stdio.h>
2 #include <fcntl.h>
3 #include <unistd.h>
4 
5 
6 int start_child(char *cmd, FILE **readpipe, FILE **writepipe);
7 
8 
main(int argc,char ** argv)9 void main(int argc, char **argv)
10 {
11    char *source_path = "/home/tkr/SYMPHONY/DrawGraph/IGD_1.0/";
12    char *graph_name = NULL;
13    FILE *read_from, *write_to;
14    int childpid;
15 
16    if (argc > 1)
17       graph_name = argv[1];
18 
19    /* fork the wish shell */
20    childpid = start_child((char *)"wish", &read_from, &write_to);
21 
22    /* source the tcl script into wish and invoke Igd_StartUp */
23    fprintf(write_to, "source %s/Init.tcl\n", source_path);
24    fprintf(write_to, "source %s/Tools.tcl\n", source_path);
25    fprintf(write_to, "source %s/NodeEdgeBasics.tcl\n", source_path);
26    fprintf(write_to, "source %s/FileMenu.tcl\n", source_path);
27    fprintf(write_to, "source %s/WindowMenu.tcl\n", source_path);
28    fprintf(write_to, "source %s/NodeMenu.tcl\n", source_path);
29    fprintf(write_to, "source %s/EdgeMenu.tcl\n", source_path);
30    fprintf(write_to, "Igd_StartUp\n");
31    fprintf(write_to, "Igd_CopyApplDefaultToWindow 1\n");
32    fprintf(write_to, "Igd_InitWindow 1 first\n");
33    fprintf(write_to, "Igd_DisplayWindow 1\n");
34    if (graph_name)
35       fprintf(write_to, "Igd_LoadGraph 1 %s\n", graph_name);
36 }
37 
38 
39 /*****************************************************************************
40  * Exec the named cmd as a child process, returning two pipes to
41  * communicate with the process, and the child's process ID.
42  *****************************************************************************/
43 
start_child(char * cmd,FILE ** readpipe,FILE ** writepipe)44 int start_child(char *cmd, FILE **readpipe, FILE **writepipe)
45 {
46    int childpid, pipe1[2], pipe2[2];
47 
48    if ((pipe(pipe1) < 0) || (pipe(pipe2) < 0)){
49       perror("pipe");
50       exit(-1);
51    }
52 
53    if ((childpid = vfork()) < 0){
54       perror("fork");
55       exit(-1);
56    }else if (childpid > 0){    /* parent */
57       close(pipe1[0]);
58       close(pipe2[1]);
59       /* write to child is pipe1[1], read from child is pipe2[0] */
60       *readpipe = fdopen(pipe2[0], "r");
61       /* this sets the pipe to be a Non-Blocking IO one, so fgets won't wait
62        * until it receives a line. */
63       fcntl(pipe2[0], F_SETFL, O_NONBLOCK);
64       *writepipe = fdopen(pipe1[1], "w");
65       setlinebuf(*writepipe);
66       return(childpid);
67    }else{      /* child */
68       close(pipe1[1]);
69       close(pipe2[0]);
70       /* read from parent is pipe1[0], write to parent is pipe2[1] */
71       dup2(pipe1[0], 0);
72       dup2(pipe2[1], 1);
73       close(pipe1[0]);
74       close(pipe2[1]);
75       if (execlp(cmd, cmd, NULL) < 0)
76 	 perror("execlp");
77 
78       /* never returns */
79    }
80    return(0);
81 }
82