00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058 #include <stdio.h>
00059 #include <signal.h>
00060 #include <sys/types.h>
00061 #include <unistd.h>
00062 #include <signal.h>
00063 #ifndef __MINGW32__
00064 #if defined(__CYGWIN__) || defined(__APPLE__)
00065 #include <sys/wait.h>
00066 #elif defined(BUILD_OS_DARWIN)
00067 #include <sys/wait.h>
00068 #else
00069 #include <wait.h>
00070 #endif
00071 #endif
00072
00073 #define USE_STANDARD_TYPES
00074 #include "defs.h"
00075 #include "mempool.h"
00076 #include "cxx_memory.h"
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091 class DAVINCI {
00092 private:
00093 BOOL display_ok;
00094 pid_t pid;
00095 FILE *to_display, *from_display;
00096
00097 void wait_for (const char *str = "ok\n");
00098 void cleanup ();
00099
00100 public:
00101 DAVINCI(void);
00102 FILE *From_display(void) { return from_display; }
00103 FILE *To_display(void) { return to_display; }
00104
00105 };
00106
00107
00108 DAVINCI::DAVINCI(void)
00109 {
00110 #ifndef __MINGW32__
00111 to_display = from_display = NULL;
00112
00113 if (isatty (1) == 0 && isatty (2) == 0)
00114 return;
00115
00116
00117 int read_pipe[2], write_pipe[2];
00118
00119 if (pipe (read_pipe) == -1 || pipe (write_pipe) == -1)
00120 return;
00121
00122 from_display = fdopen (read_pipe[0], "r");
00123 setbuf (from_display, NULL);
00124 to_display = fdopen (write_pipe[1], "w");
00125 setbuf (to_display, NULL);
00126
00127 switch (pid = fork ()) {
00128 case -1:
00129 close (read_pipe[0]);
00130 close (read_pipe[1]);
00131 close (write_pipe[0]);
00132 close (write_pipe[1]);
00133 return;
00134
00135 case 0:
00136 dup2 (write_pipe[0], 0);
00137 dup2 (read_pipe[1], 1);
00138 dup2 (read_pipe[1], 2);
00139
00140 close (write_pipe[0]);
00141 close (read_pipe[1]);
00142
00143
00144
00145 execlp ("daVinci", "daVinci", "-pipe", NULL);
00146 fprintf(stderr,"cannot found daVinci.\n");
00147 exit (1);
00148
00149 default:
00150 close (read_pipe[1]);
00151 close (write_pipe[0]);
00152 wait_for ();
00153
00154 if (display_ok) {
00155
00156 fprintf(to_display, "set(font_size(8))\n");
00157 fprintf(to_display, "set(gap_height(4))\n");
00158 fprintf(to_display, "set(gap_width(20))\n");
00159 fprintf(to_display, "menu(view_menu(fit_scale_to_window))\n");
00160 }
00161 }
00162 #endif
00163 }
00164
00165
00166
00167 void
00168 DAVINCI::wait_for (const char *str)
00169 {
00170 char buf[512];
00171
00172 while (fgets (buf, 512, from_display) != NULL) {
00173 if (strcmp (buf, "ok\n") == 0) {
00174 display_ok = TRUE;
00175 return;
00176 } else {
00177 switch (buf[0]) {
00178 case 'n':
00179 case 'e':
00180 case 'm':
00181 case 'a':
00182 if (strncmp (buf, str, strlen(str)) == 0) {
00183 display_ok = TRUE;
00184 return;
00185 }
00186 break;
00187 default:
00188 fputs (buf, stderr);
00189 cleanup ();
00190 return;
00191 }
00192 }
00193 }
00194
00195 cleanup ();
00196 return;
00197 }
00198
00199
00200 void
00201 DAVINCI::cleanup (void)
00202 {
00203 #ifndef __MINGW32__
00204 int stat;
00205
00206 display_ok = FALSE;
00207 kill (pid, SIGINT);
00208 waitpid (pid, &stat, WNOHANG);
00209
00210 fclose (to_display);
00211 fclose (from_display);
00212 #endif
00213 }
00214
00215
00216 FILE *
00217 Init_daVinci(void)
00218 {
00219 static DAVINCI *daVinci = NULL;
00220
00221 if (daVinci == NULL) {
00222 daVinci = CXX_NEW(DAVINCI(), Malloc_Mem_Pool);
00223 }
00224 return daVinci->To_display();
00225 }
00226
00227 FILE *
00228 New_daVinci(void)
00229 {
00230 DAVINCI *daVinci = CXX_NEW(DAVINCI(), Malloc_Mem_Pool);
00231 return daVinci->To_display();
00232 }
00233