Annotation of OpenXM_contrib2/windows/asir32gui/io.c, Revision 1.3
1.1 noro 1: #include <windows.h>
2: #include <stdlib.h>
3: #include <stdio.h>
4: #include <io.h>
5: #include <fcntl.h>
6: #include <process.h>
7:
1.2 noro 8: #include "asir32gui_io.h"
9:
1.1 noro 10: #define ECGEN_KEYNAME "SoftWare\\Fujitsu\\FSEcParamGen\\V1.0L10"
11: #define ASIR_KEYNAME "SoftWare\\Fujitsu\\Asir\\1999.03.31"
12:
13: #define MAXHIST 256
14: #define HISTORY "history"
15:
16: static int use_current_dir;
17:
1.2 noro 18: int asirgui_kind;
1.1 noro 19: HANDLE hProc;
20: static HANDLE hRead,hWrite;
21: HANDLE hNotify,hNotify_Ack;
22: static HANDLE hIntr,hIntr_Ack,hKill;
23:
24: void _setargv(void);
25: void init_input_history();
26: void write_input_history();
27: void read_input_history();
28: void init_hist(int);
29: void read_hist(char *);
30: void write_hist(char *);
31: void show_window(int);
32:
33: BOOL get_rootdir(name,len,errmsg)
34: char *name;
35: int len;
36: char *errmsg;
37: {
38: LONG ret;
39: HKEY hOpenKey;
40: DWORD Type,dw;
41: char dir[BUFSIZ],message[BUFSIZ];
42: char *slash;
43: int ldir;
44: static char rootdir[BUFSIZ];
45: static int rootdir_is_initialized;
46:
47: if ( rootdir_is_initialized ) {
48: strcpy(name,rootdir);
49: return TRUE;
50: }
51:
1.2 noro 52: GetCurrentDirectory(BUFSIZ,dir);
53: slash = strrchr(dir,'\\');
54: if ( slash )
55: *slash = 0;
56: ldir = strlen(dir)+1;
1.3 ! noro 57: if ( 1 || access("UseCurrentDir",0) >= 0 ) {
1.2 noro 58: use_current_dir = 1;
59: strcpy(rootdir,dir);
60: strcpy(name,dir);
61: rootdir_is_initialized = 1;
62: return TRUE;
63: }
1.1 noro 64: ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, ECGEN_KEYNAME, 0,
65: KEY_QUERY_VALUE, &hOpenKey);
66: if ( ret != ERROR_SUCCESS )
67: ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, ASIR_KEYNAME, 0,
68: KEY_QUERY_VALUE, &hOpenKey);
69: if( ret != ERROR_SUCCESS ) {
70: sprintf(message,"May I register '%s' as the ASIR main directory?",dir);
71: if( IDNO == MessageBox(NULL,message,"Asir",
72: MB_YESNO | MB_ICONEXCLAMATION | MB_DEFBUTTON2) ) {
73: sprintf(errmsg,"The ASIR main directory was not set.");
74: return FALSE;
75: }
76: if ( ret = RegCreateKeyEx(HKEY_LOCAL_MACHINE,ASIR_KEYNAME,0,
77: REG_NONE,REG_OPTION_NON_VOLATILE,KEY_READ|KEY_WRITE,NULL,&hOpenKey,&dw) ) {
78: FormatMessage(
79: FORMAT_MESSAGE_FROM_SYSTEM |
80: FORMAT_MESSAGE_IGNORE_INSERTS,
81: NULL,
82: ret,
83: MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
84: (LPSTR)errmsg,
85: BUFSIZ,
86: NULL);
87: return FALSE;
88: }
89: if ( ret = RegSetValueEx(hOpenKey,"Directory",NULL,REG_SZ,dir,ldir) ) {
90: FormatMessage(
91: FORMAT_MESSAGE_FROM_SYSTEM |
92: FORMAT_MESSAGE_IGNORE_INSERTS,
93: NULL,
94: ret,
95: MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
96: (LPSTR)errmsg,
97: BUFSIZ,
98: NULL);
99: return FALSE;
100: }
101: }
102: RegQueryValueEx(hOpenKey, "Directory", NULL, &Type, name, &len);
103: RegCloseKey(hOpenKey);
104: strcpy(rootdir,name);
105: rootdir_is_initialized = 1;
106: return TRUE;
107: }
108:
109: BOOL Init_IO(char *errmsg) {
110: int i;
111: char *av[BUFSIZ];
112: char AsirExe[BUFSIZ];
113: DWORD mypid,len;
114: HANDLE hR0,hW0,hR1,hW1;
115: SECURITY_ATTRIBUTES SecurityAttributes;
116: char remread[10],remwrite[10];
117: char notify[100],notify_ack[100],intr[100],intr_ack[100],kill[100];
118: char name[BUFSIZ],qname[BUFSIZ],home[BUFSIZ];
119:
120: _setargv();
121: if ( !strcmp(__argv[0],"debuggui") ) {
1.2 noro 122: asirgui_kind = ASIRGUI_DEBUG;
1.1 noro 123: hRead = atoi(__argv[1]);
124: hWrite = atoi(__argv[2]);
125: hNotify = OpenEvent(EVENT_ALL_ACCESS|EVENT_MODIFY_STATE,TRUE,__argv[3]);
126: hNotify_Ack = OpenEvent(EVENT_ALL_ACCESS|EVENT_MODIFY_STATE,TRUE,__argv[4]);
127: hIntr = OpenEvent(EVENT_ALL_ACCESS|EVENT_MODIFY_STATE,TRUE,__argv[5]);
128: hIntr_Ack = OpenEvent(EVENT_ALL_ACCESS|EVENT_MODIFY_STATE,TRUE,__argv[6]);
129: hKill = OpenEvent(EVENT_ALL_ACCESS|EVENT_MODIFY_STATE,TRUE,__argv[7]);
130: return TRUE;
131: } else if ( !strcmp(__argv[0],"messagegui") ) {
1.2 noro 132: asirgui_kind = ASIRGUI_MESSAGE;
1.1 noro 133: hRead = atoi(__argv[1]);
134: hWrite = atoi(__argv[2]);
135: hNotify = OpenEvent(EVENT_ALL_ACCESS|EVENT_MODIFY_STATE,TRUE,__argv[3]);
136: hNotify_Ack = OpenEvent(EVENT_ALL_ACCESS|EVENT_MODIFY_STATE,TRUE,__argv[4]);
137: return TRUE;
138: } else {
1.2 noro 139: asirgui_kind = ASIRGUI_MAIN;
1.1 noro 140: mypid = GetCurrentProcessId();
141: sprintf(notify,"asir_notify_%d",mypid);
142: sprintf(notify_ack,"asir_notify_ack_%d",mypid);
143: sprintf(intr,"asir_intr_%d",mypid);
144: sprintf(intr_ack,"asir_intr_ack_%d",mypid);
145: sprintf(kill,"asir_kill_%d",mypid);
146: hNotify = CreateEvent(NULL,TRUE,FALSE,notify);
147: hNotify_Ack = CreateEvent(NULL,TRUE,FALSE,notify_ack);
148: hIntr = CreateEvent(NULL,TRUE,FALSE,intr);
149: hIntr_Ack = CreateEvent(NULL,TRUE,FALSE,intr_ack);
150: hKill = CreateEvent(NULL,TRUE,FALSE,kill);
151:
152: SecurityAttributes.nLength = sizeof(SecurityAttributes);
153: SecurityAttributes.lpSecurityDescriptor = NULL;
154: SecurityAttributes.bInheritHandle = TRUE;
155: CreatePipe(&hR0, &hW0, &SecurityAttributes, 65536);
156: CreatePipe(&hR1, &hW1, &SecurityAttributes, 65536);
157:
158: hRead = hR0;
159: hWrite = hW1;
160: sprintf(remread,"%d",(DWORD)hR1);
161: sprintf(remwrite,"%d",(DWORD)hW0);
162:
163: len = sizeof(name);
164: if ( get_rootdir(name,len,errmsg) == FALSE ) {
165: return FALSE;
166: }
1.3 ! noro 167: // sprintf(AsirExe,"%s\\bin\\engine.exe",name);
1.1 noro 168: // sprintf(AsirExe,"c:\\home\\noro\\engine2000\\debug\\engine.exe");
1.3 ! noro 169: sprintf(AsirExe,"d:\\cygwin\\home\\noro\\OpenXM_contrib2\\\\windows\\engine2000\\debug\\engine.exe");
1.1 noro 170: if ( !getenv("HOME") ) {
171: sprintf(home,"HOME=%s",name);
172: _putenv(home);
173: }
174: av[0] = "asir";
175: av[1] = remread;
176: av[2] = remwrite;
177: av[3] = notify;
178: av[4] = notify_ack;
179: av[5] = intr;
180: av[6] = intr_ack;
181: av[7] = kill;
182: for ( i = 1; i < __argc; i++ )
183: av[i+7] = __argv[i];
184: if ( use_current_dir ) {
185: av[i+7] = "-rootdir"; i++;
186: sprintf(qname,"\"%s\"",name);
187: av[i+7] = qname; i++;
188: }
189: av[i+7] = NULL;
190: hProc = (HANDLE)_spawnv(_P_NOWAIT,AsirExe,av);
191: if ( hProc == (HANDLE)-1 ) {
192: sprintf(errmsg,"%s is not found.",AsirExe);
193: return FALSE;
194: }
195: return TRUE;
196: }
197: }
198:
199: void put_line(char *buf) {
200: DWORD len;
201: int size = strlen(buf);
202: buf[size] = '\n';
203: buf[size+1] = 0;
204: size++;
205: WriteFile(hWrite,&size,sizeof(int),&len,NULL);
206: WriteFile(hWrite,buf,size,&len,NULL);
207: }
208:
209: #define FROMASIR_EXIT 0
210: #define FROMASIR_TEXT 1
211: #define FROMASIR_HEAPSIZE 2
212: #define FROMASIR_SHOW 3
213: #define FROMASIR_HIDE 4
214: #define FROMASIR_DEBUGPROC 5
215:
216: static char insert_buf[BUFSIZ*10];
217: static int insert_buf_len = 0;
218:
219: int AsirHeapSize;
220:
221: void insert_to_theView(char *);
222: void flush_log();
223:
224: void read_and_insert()
225: {
226: DWORD len;
227: unsigned int cmdsize,cmd,size;
228:
229: ReadFile(hRead,&cmdsize,sizeof(unsigned int),&len,NULL);
230: cmd = cmdsize>>16;
231: size = cmdsize&0xffff;
232:
233: switch ( cmd ) {
234: case FROMASIR_EXIT:
235: write_input_history();
236: flush_log();
237: PostMessage(NULL,WM_QUIT,0,0);
238: break;
239: case FROMASIR_TEXT:
240: if ( size ) {
241: ReadFile(hRead,insert_buf,size,&len,NULL);
242: insert_buf[size] = 0;
243: insert_to_theView(insert_buf);
244: }
245: break;
246: case FROMASIR_HEAPSIZE:
247: if ( size )
248: ReadFile(hRead,&AsirHeapSize,size,&len,NULL);
249: break;
250: case FROMASIR_SHOW:
251: show_window(1);
252: break;
253: case FROMASIR_HIDE:
254: show_window(0);
255: break;
256: default:
257: if ( size )
258: ReadFile(hRead,insert_buf,size,&len,NULL);
259: break;
260: }
261: }
262:
263: void init_input_history()
264: {
265: init_hist(MAXHIST);
266: }
267:
268: void write_input_history()
269: {
270: write_hist(HISTORY);
271: }
272:
273: void read_input_history()
274: {
275: read_hist(HISTORY);
276: }
277:
278: void send_intr() {
279: PulseEvent(hIntr);
280: }
281:
282: void terminate_asir() {
283: PulseEvent(hKill);
284: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>