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