Annotation of OpenXM_contrib2/windows/asir32gui/io.c, Revision 1.7
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;
1.6 ohara 41: char dir[BUFSIZ],message[BUFSIZ],engine[BUFSIZ];
1.1 noro 42: char *slash;
1.6 ohara 43: size_t ldir;
1.1 noro 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.6 ohara 52: if(GetModuleFileName(NULL,dir,BUFSIZ)) {
53: slash = strrchr(dir,'\\');
54: *slash = 0;
55: sprintf(engine,"%s\\engine.exe", dir);
56: if ( access(engine,0) >= 0 ) {
57: slash = strrchr(dir,'\\');
58: if ( slash ) {
59: *slash = 0;
60: }
61: use_current_dir = 1;
62: strcpy(rootdir,dir);
63: strcpy(name,dir);
64: rootdir_is_initialized = 1;
65: return TRUE;
66: }
67: }
1.2 noro 68: GetCurrentDirectory(BUFSIZ,dir);
69: slash = strrchr(dir,'\\');
70: if ( slash )
71: *slash = 0;
72: ldir = strlen(dir)+1;
1.4 noro 73: if ( access("UseCurrentDir",0) >= 0 ) {
1.2 noro 74: use_current_dir = 1;
75: strcpy(rootdir,dir);
76: strcpy(name,dir);
77: rootdir_is_initialized = 1;
78: return TRUE;
79: }
1.1 noro 80: ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, ECGEN_KEYNAME, 0,
81: KEY_QUERY_VALUE, &hOpenKey);
82: if ( ret != ERROR_SUCCESS )
83: ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, ASIR_KEYNAME, 0,
84: KEY_QUERY_VALUE, &hOpenKey);
85: if( ret != ERROR_SUCCESS ) {
86: sprintf(message,"May I register '%s' as the ASIR main directory?",dir);
87: if( IDNO == MessageBox(NULL,message,"Asir",
88: MB_YESNO | MB_ICONEXCLAMATION | MB_DEFBUTTON2) ) {
89: sprintf(errmsg,"The ASIR main directory was not set.");
90: return FALSE;
91: }
92: if ( ret = RegCreateKeyEx(HKEY_LOCAL_MACHINE,ASIR_KEYNAME,0,
93: REG_NONE,REG_OPTION_NON_VOLATILE,KEY_READ|KEY_WRITE,NULL,&hOpenKey,&dw) ) {
94: FormatMessage(
95: FORMAT_MESSAGE_FROM_SYSTEM |
96: FORMAT_MESSAGE_IGNORE_INSERTS,
97: NULL,
98: ret,
99: MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
100: (LPSTR)errmsg,
101: BUFSIZ,
102: NULL);
103: return FALSE;
104: }
1.6 ohara 105: if ( ret = RegSetValueEx(hOpenKey,"Directory",0,REG_SZ,dir,ldir) ) {
1.1 noro 106: FormatMessage(
107: FORMAT_MESSAGE_FROM_SYSTEM |
108: FORMAT_MESSAGE_IGNORE_INSERTS,
109: NULL,
110: ret,
111: MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
112: (LPSTR)errmsg,
113: BUFSIZ,
114: NULL);
115: return FALSE;
116: }
117: }
118: RegQueryValueEx(hOpenKey, "Directory", NULL, &Type, name, &len);
119: RegCloseKey(hOpenKey);
120: strcpy(rootdir,name);
121: rootdir_is_initialized = 1;
122: return TRUE;
123: }
124:
1.7 ! ohara 125: const char *get_helpdir() {
! 126: static char helpdir[BUFSIZ];
! 127: static int helpdir_is_initialized;
! 128: char lang[BUFSIZ];
! 129: char file[BUFSIZ];
! 130: char root[BUFSIZ];
! 131: char errmsg[BUFSIZ];
! 132:
! 133: if( !helpdir_is_initialized ) {
! 134: get_rootdir(root,sizeof(root),errmsg);
! 135: GetLocaleInfo(GetUserDefaultLCID(), LOCALE_SISO639LANGNAME, lang, BUFSIZ);
! 136: sprintf(helpdir, "%s\\help\\%s", root, lang);
! 137: sprintf(file, "%s\\asirhelp.chm", helpdir);
! 138: if ( access(file,0) < 0 ) {
! 139: sprintf(helpdir, "%s\\help", root);
! 140: }
! 141: helpdir_is_initialized = 1;
! 142: }
! 143: return helpdir;
! 144: }
! 145:
1.1 noro 146: BOOL Init_IO(char *errmsg) {
147: int i;
148: char *av[BUFSIZ];
149: char AsirExe[BUFSIZ];
150: DWORD mypid,len;
151: HANDLE hR0,hW0,hR1,hW1;
152: SECURITY_ATTRIBUTES SecurityAttributes;
153: char remread[10],remwrite[10];
154: char notify[100],notify_ack[100],intr[100],intr_ack[100],kill[100];
155: char name[BUFSIZ],qname[BUFSIZ],home[BUFSIZ];
156:
157: _setargv();
158: if ( !strcmp(__argv[0],"debuggui") ) {
1.2 noro 159: asirgui_kind = ASIRGUI_DEBUG;
1.1 noro 160: hRead = atoi(__argv[1]);
161: hWrite = atoi(__argv[2]);
162: hNotify = OpenEvent(EVENT_ALL_ACCESS|EVENT_MODIFY_STATE,TRUE,__argv[3]);
163: hNotify_Ack = OpenEvent(EVENT_ALL_ACCESS|EVENT_MODIFY_STATE,TRUE,__argv[4]);
164: hIntr = OpenEvent(EVENT_ALL_ACCESS|EVENT_MODIFY_STATE,TRUE,__argv[5]);
165: hIntr_Ack = OpenEvent(EVENT_ALL_ACCESS|EVENT_MODIFY_STATE,TRUE,__argv[6]);
166: hKill = OpenEvent(EVENT_ALL_ACCESS|EVENT_MODIFY_STATE,TRUE,__argv[7]);
167: return TRUE;
168: } else if ( !strcmp(__argv[0],"messagegui") ) {
1.2 noro 169: asirgui_kind = ASIRGUI_MESSAGE;
1.1 noro 170: hRead = atoi(__argv[1]);
171: hWrite = atoi(__argv[2]);
172: hNotify = OpenEvent(EVENT_ALL_ACCESS|EVENT_MODIFY_STATE,TRUE,__argv[3]);
173: hNotify_Ack = OpenEvent(EVENT_ALL_ACCESS|EVENT_MODIFY_STATE,TRUE,__argv[4]);
174: return TRUE;
175: } else {
1.2 noro 176: asirgui_kind = ASIRGUI_MAIN;
1.1 noro 177: mypid = GetCurrentProcessId();
178: sprintf(notify,"asir_notify_%d",mypid);
179: sprintf(notify_ack,"asir_notify_ack_%d",mypid);
180: sprintf(intr,"asir_intr_%d",mypid);
181: sprintf(intr_ack,"asir_intr_ack_%d",mypid);
182: sprintf(kill,"asir_kill_%d",mypid);
183: hNotify = CreateEvent(NULL,TRUE,FALSE,notify);
184: hNotify_Ack = CreateEvent(NULL,TRUE,FALSE,notify_ack);
185: hIntr = CreateEvent(NULL,TRUE,FALSE,intr);
186: hIntr_Ack = CreateEvent(NULL,TRUE,FALSE,intr_ack);
187: hKill = CreateEvent(NULL,TRUE,FALSE,kill);
188:
189: SecurityAttributes.nLength = sizeof(SecurityAttributes);
190: SecurityAttributes.lpSecurityDescriptor = NULL;
191: SecurityAttributes.bInheritHandle = TRUE;
192: CreatePipe(&hR0, &hW0, &SecurityAttributes, 65536);
193: CreatePipe(&hR1, &hW1, &SecurityAttributes, 65536);
194:
195: hRead = hR0;
196: hWrite = hW1;
197: sprintf(remread,"%d",(DWORD)hR1);
198: sprintf(remwrite,"%d",(DWORD)hW0);
199:
200: len = sizeof(name);
201: if ( get_rootdir(name,len,errmsg) == FALSE ) {
202: return FALSE;
203: }
1.4 noro 204: sprintf(AsirExe,"%s\\bin\\engine.exe",name);
1.1 noro 205: // sprintf(AsirExe,"c:\\home\\noro\\engine2000\\debug\\engine.exe");
206: if ( !getenv("HOME") ) {
207: sprintf(home,"HOME=%s",name);
208: _putenv(home);
209: }
210: av[0] = "asir";
211: av[1] = remread;
212: av[2] = remwrite;
213: av[3] = notify;
214: av[4] = notify_ack;
215: av[5] = intr;
216: av[6] = intr_ack;
217: av[7] = kill;
218: for ( i = 1; i < __argc; i++ )
219: av[i+7] = __argv[i];
220: if ( use_current_dir ) {
221: av[i+7] = "-rootdir"; i++;
222: sprintf(qname,"\"%s\"",name);
223: av[i+7] = qname; i++;
224: }
225: av[i+7] = NULL;
226: hProc = (HANDLE)_spawnv(_P_NOWAIT,AsirExe,av);
227: if ( hProc == (HANDLE)-1 ) {
228: sprintf(errmsg,"%s is not found.",AsirExe);
229: return FALSE;
230: }
231: return TRUE;
232: }
233: }
234:
235: void put_line(char *buf) {
236: DWORD len;
237: int size = strlen(buf);
238: buf[size] = '\n';
239: buf[size+1] = 0;
240: size++;
241: WriteFile(hWrite,&size,sizeof(int),&len,NULL);
242: WriteFile(hWrite,buf,size,&len,NULL);
243: }
244:
245: #define FROMASIR_EXIT 0
246: #define FROMASIR_TEXT 1
247: #define FROMASIR_HEAPSIZE 2
248: #define FROMASIR_SHOW 3
249: #define FROMASIR_HIDE 4
250: #define FROMASIR_DEBUGPROC 5
251:
252: static char insert_buf[BUFSIZ*10];
253: static int insert_buf_len = 0;
254:
255: int AsirHeapSize;
256:
257: void insert_to_theView(char *);
258: void flush_log();
259:
260: void read_and_insert()
261: {
262: DWORD len;
263: unsigned int cmdsize,cmd,size;
264:
265: ReadFile(hRead,&cmdsize,sizeof(unsigned int),&len,NULL);
266: cmd = cmdsize>>16;
267: size = cmdsize&0xffff;
268:
269: switch ( cmd ) {
270: case FROMASIR_EXIT:
271: write_input_history();
272: flush_log();
273: PostMessage(NULL,WM_QUIT,0,0);
274: break;
275: case FROMASIR_TEXT:
276: if ( size ) {
277: ReadFile(hRead,insert_buf,size,&len,NULL);
278: insert_buf[size] = 0;
279: insert_to_theView(insert_buf);
280: }
281: break;
282: case FROMASIR_HEAPSIZE:
283: if ( size )
284: ReadFile(hRead,&AsirHeapSize,size,&len,NULL);
285: break;
286: case FROMASIR_SHOW:
287: show_window(1);
288: break;
289: case FROMASIR_HIDE:
290: show_window(0);
291: break;
292: default:
293: if ( size )
294: ReadFile(hRead,insert_buf,size,&len,NULL);
295: break;
296: }
297: }
298:
1.5 noro 299: static char history_path[BUFSIZ];
300: static char history_errmsg[BUFSIZ];
301:
1.1 noro 302: void init_input_history()
303: {
1.5 noro 304: char rootdir[BUFSIZ];
305: get_rootdir(rootdir,sizeof(rootdir),history_errmsg);
306: sprintf(history_path,"%s\\bin\\%s",rootdir,HISTORY);
1.1 noro 307: init_hist(MAXHIST);
308: }
309:
310: void write_input_history()
311: {
1.5 noro 312: write_hist(history_path);
1.1 noro 313: }
314:
315: void read_input_history()
316: {
1.5 noro 317: read_hist(history_path);
1.1 noro 318: }
319:
320: void send_intr() {
321: PulseEvent(hIntr);
322: }
323:
324: void terminate_asir() {
325: PulseEvent(hKill);
326: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>