Annotation of OpenXM_contrib2/windows/asir32gui/io.c, Revision 1.2
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;
! 57: if ( access("UseCurrentDir",0) >= 0 ) {
! 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: }
167: sprintf(AsirExe,"%s\\bin\\engine.exe",name);
168: // sprintf(AsirExe,"c:\\home\\noro\\engine2000\\debug\\engine.exe");
169: if ( !getenv("HOME") ) {
170: sprintf(home,"HOME=%s",name);
171: _putenv(home);
172: }
173: av[0] = "asir";
174: av[1] = remread;
175: av[2] = remwrite;
176: av[3] = notify;
177: av[4] = notify_ack;
178: av[5] = intr;
179: av[6] = intr_ack;
180: av[7] = kill;
181: for ( i = 1; i < __argc; i++ )
182: av[i+7] = __argv[i];
183: if ( use_current_dir ) {
184: av[i+7] = "-rootdir"; i++;
185: sprintf(qname,"\"%s\"",name);
186: av[i+7] = qname; i++;
187: }
188: av[i+7] = NULL;
189: hProc = (HANDLE)_spawnv(_P_NOWAIT,AsirExe,av);
190: if ( hProc == (HANDLE)-1 ) {
191: sprintf(errmsg,"%s is not found.",AsirExe);
192: return FALSE;
193: }
194: return TRUE;
195: }
196: }
197:
198: void put_line(char *buf) {
199: DWORD len;
200: int size = strlen(buf);
201: buf[size] = '\n';
202: buf[size+1] = 0;
203: size++;
204: WriteFile(hWrite,&size,sizeof(int),&len,NULL);
205: WriteFile(hWrite,buf,size,&len,NULL);
206: }
207:
208: #define FROMASIR_EXIT 0
209: #define FROMASIR_TEXT 1
210: #define FROMASIR_HEAPSIZE 2
211: #define FROMASIR_SHOW 3
212: #define FROMASIR_HIDE 4
213: #define FROMASIR_DEBUGPROC 5
214:
215: static char insert_buf[BUFSIZ*10];
216: static int insert_buf_len = 0;
217:
218: int AsirHeapSize;
219:
220: void insert_to_theView(char *);
221: void flush_log();
222:
223: void read_and_insert()
224: {
225: DWORD len;
226: unsigned int cmdsize,cmd,size;
227:
228: ReadFile(hRead,&cmdsize,sizeof(unsigned int),&len,NULL);
229: cmd = cmdsize>>16;
230: size = cmdsize&0xffff;
231:
232: switch ( cmd ) {
233: case FROMASIR_EXIT:
234: write_input_history();
235: flush_log();
236: PostMessage(NULL,WM_QUIT,0,0);
237: break;
238: case FROMASIR_TEXT:
239: if ( size ) {
240: ReadFile(hRead,insert_buf,size,&len,NULL);
241: insert_buf[size] = 0;
242: insert_to_theView(insert_buf);
243: }
244: break;
245: case FROMASIR_HEAPSIZE:
246: if ( size )
247: ReadFile(hRead,&AsirHeapSize,size,&len,NULL);
248: break;
249: case FROMASIR_SHOW:
250: show_window(1);
251: break;
252: case FROMASIR_HIDE:
253: show_window(0);
254: break;
255: default:
256: if ( size )
257: ReadFile(hRead,insert_buf,size,&len,NULL);
258: break;
259: }
260: }
261:
262: void init_input_history()
263: {
264: init_hist(MAXHIST);
265: }
266:
267: void write_input_history()
268: {
269: write_hist(HISTORY);
270: }
271:
272: void read_input_history()
273: {
274: read_hist(HISTORY);
275: }
276:
277: void send_intr() {
278: PulseEvent(hIntr);
279: }
280:
281: void terminate_asir() {
282: PulseEvent(hKill);
283: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>