[BACK]Return to io.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / windows / asir32gui

Annotation of OpenXM_contrib2/windows/asir32gui/io.c, Revision 1.12

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
1.10      ohara      14: #define HISTORY "asirgui_history"
1.1       noro       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:
1.12    ! ohara     157: #if _MSC_VER < 1910
1.1       noro      158:        _setargv();
1.11      ohara     159: #endif
1.1       noro      160:        if ( !strcmp(__argv[0],"debuggui") ) {
1.2       noro      161:                asirgui_kind = ASIRGUI_DEBUG;
1.1       noro      162:                hRead = atoi(__argv[1]);
                    163:                hWrite = atoi(__argv[2]);
                    164:                hNotify = OpenEvent(EVENT_ALL_ACCESS|EVENT_MODIFY_STATE,TRUE,__argv[3]);
                    165:                hNotify_Ack = OpenEvent(EVENT_ALL_ACCESS|EVENT_MODIFY_STATE,TRUE,__argv[4]);
                    166:                hIntr = OpenEvent(EVENT_ALL_ACCESS|EVENT_MODIFY_STATE,TRUE,__argv[5]);
                    167:                hIntr_Ack = OpenEvent(EVENT_ALL_ACCESS|EVENT_MODIFY_STATE,TRUE,__argv[6]);
                    168:                hKill = OpenEvent(EVENT_ALL_ACCESS|EVENT_MODIFY_STATE,TRUE,__argv[7]);
                    169:                return TRUE;
                    170:        } else if ( !strcmp(__argv[0],"messagegui") ) {
1.2       noro      171:                asirgui_kind = ASIRGUI_MESSAGE;
1.1       noro      172:                hRead = atoi(__argv[1]);
                    173:                hWrite = atoi(__argv[2]);
                    174:                hNotify = OpenEvent(EVENT_ALL_ACCESS|EVENT_MODIFY_STATE,TRUE,__argv[3]);
                    175:                hNotify_Ack = OpenEvent(EVENT_ALL_ACCESS|EVENT_MODIFY_STATE,TRUE,__argv[4]);
                    176:                return TRUE;
                    177:        } else {
1.2       noro      178:                asirgui_kind = ASIRGUI_MAIN;
1.1       noro      179:                mypid = GetCurrentProcessId();
                    180:                sprintf(notify,"asir_notify_%d",mypid);
                    181:                sprintf(notify_ack,"asir_notify_ack_%d",mypid);
                    182:                sprintf(intr,"asir_intr_%d",mypid);
                    183:                sprintf(intr_ack,"asir_intr_ack_%d",mypid);
                    184:                sprintf(kill,"asir_kill_%d",mypid);
                    185:                hNotify = CreateEvent(NULL,TRUE,FALSE,notify);
                    186:                hNotify_Ack = CreateEvent(NULL,TRUE,FALSE,notify_ack);
                    187:                hIntr = CreateEvent(NULL,TRUE,FALSE,intr);
                    188:                hIntr_Ack = CreateEvent(NULL,TRUE,FALSE,intr_ack);
                    189:                hKill = CreateEvent(NULL,TRUE,FALSE,kill);
                    190:
                    191:        SecurityAttributes.nLength = sizeof(SecurityAttributes);
                    192:        SecurityAttributes.lpSecurityDescriptor = NULL;
                    193:        SecurityAttributes.bInheritHandle = TRUE;
                    194:        CreatePipe(&hR0, &hW0, &SecurityAttributes, 65536);
                    195:        CreatePipe(&hR1, &hW1, &SecurityAttributes, 65536);
                    196:
                    197:                hRead = hR0;
                    198:                hWrite = hW1;
                    199:                sprintf(remread,"%d",(DWORD)hR1);
                    200:                sprintf(remwrite,"%d",(DWORD)hW0);
                    201:
                    202:                len = sizeof(name);
                    203:                if ( get_rootdir(name,len,errmsg) == FALSE ) {
                    204:                        return FALSE;
                    205:                }
1.9       noro      206:                sprintf(AsirExe,"%s\\bin\\engine.exe",name);
                    207: //             sprintf(AsirExe,"d:\\cygwin\\home\\noro\\new\\OpenXM_contrib2\\windows\\engine2000\\debug\\engine.exe");
1.1       noro      208:                if ( !getenv("HOME") ) {
                    209:                        sprintf(home,"HOME=%s",name);
                    210:                        _putenv(home);
                    211:                }
                    212:                av[0] = "asir";
                    213:                av[1] = remread;
                    214:                av[2] = remwrite;
                    215:                av[3] = notify;
                    216:                av[4] = notify_ack;
                    217:                av[5] = intr;
                    218:                av[6] = intr_ack;
                    219:                av[7] = kill;
                    220:                for ( i = 1; i < __argc; i++ )
                    221:                        av[i+7] = __argv[i];
                    222:                if ( use_current_dir )  {
                    223:                        av[i+7] = "-rootdir"; i++;
                    224:                        sprintf(qname,"\"%s\"",name);
                    225:                        av[i+7] = qname; i++;
                    226:                }
                    227:                av[i+7] = NULL;
                    228:                hProc = (HANDLE)_spawnv(_P_NOWAIT,AsirExe,av);
                    229:                if ( hProc == (HANDLE)-1 ) {
                    230:                        sprintf(errmsg,"%s is not found.",AsirExe);
                    231:                        return FALSE;
                    232:                }
                    233:                return TRUE;
                    234:        }
                    235: }
                    236:
                    237: void put_line(char *buf) {
                    238:        DWORD len;
                    239:        int size = strlen(buf);
                    240:        buf[size] = '\n';
                    241:        buf[size+1] = 0;
                    242:        size++;
                    243:        WriteFile(hWrite,&size,sizeof(int),&len,NULL);
                    244:        WriteFile(hWrite,buf,size,&len,NULL);
                    245: }
                    246:
                    247: #define FROMASIR_EXIT 0
                    248: #define FROMASIR_TEXT 1
                    249: #define FROMASIR_HEAPSIZE 2
                    250: #define FROMASIR_SHOW 3
                    251: #define FROMASIR_HIDE 4
                    252: #define FROMASIR_DEBUGPROC 5
                    253:
                    254: static char insert_buf[BUFSIZ*10];
                    255: static int insert_buf_len = 0;
                    256:
                    257: int AsirHeapSize;
                    258:
                    259: void insert_to_theView(char *);
                    260: void flush_log();
                    261:
                    262: void read_and_insert()
                    263: {
                    264:        DWORD len;
                    265:        unsigned int cmdsize,cmd,size;
                    266:
                    267:        ReadFile(hRead,&cmdsize,sizeof(unsigned int),&len,NULL);
                    268:        cmd = cmdsize>>16;
                    269:        size = cmdsize&0xffff;
                    270:
                    271:        switch ( cmd ) {
                    272:                case FROMASIR_EXIT:
                    273:                        write_input_history();
                    274:                        flush_log();
                    275:                        PostMessage(NULL,WM_QUIT,0,0);
                    276:                        break;
                    277:                case FROMASIR_TEXT:
                    278:                        if ( size ) {
                    279:                                ReadFile(hRead,insert_buf,size,&len,NULL);
                    280:                                insert_buf[size] = 0;
                    281:                                insert_to_theView(insert_buf);
                    282:                        }
                    283:                        break;
                    284:                case FROMASIR_HEAPSIZE:
                    285:                        if ( size )
                    286:                                ReadFile(hRead,&AsirHeapSize,size,&len,NULL);
                    287:                        break;
                    288:                case FROMASIR_SHOW:
                    289:                        show_window(1);
                    290:                        break;
                    291:                case FROMASIR_HIDE:
                    292:                        show_window(0);
                    293:                        break;
                    294:                default:
                    295:                        if ( size )
                    296:                                ReadFile(hRead,insert_buf,size,&len,NULL);
                    297:                        break;
                    298:        }
                    299: }
                    300:
1.5       noro      301: static char history_path[BUFSIZ];
                    302: static char history_errmsg[BUFSIZ];
                    303:
1.1       noro      304: void init_input_history()
                    305: {
1.5       noro      306:        char rootdir[BUFSIZ];
1.10      ohara     307:        char *env = getenv("TEMP");
                    308:        if( env ) {
                    309:                sprintf(history_path,"%s\\%s",env,HISTORY);
                    310:        }else {
                    311:                get_rootdir(rootdir,sizeof(rootdir),history_errmsg);
                    312:                sprintf(history_path,"%s\\bin\\%s",rootdir,HISTORY);
                    313:        }
1.1       noro      314:        init_hist(MAXHIST);
                    315: }
                    316:
                    317: void write_input_history()
                    318: {
1.5       noro      319:        write_hist(history_path);
1.1       noro      320: }
                    321:
                    322: void read_input_history()
                    323: {
1.5       noro      324:        read_hist(history_path);
1.1       noro      325: }
                    326:
                    327: void send_intr() {
1.8       noro      328:        HANDLE handle[1];
                    329:
1.1       noro      330:        PulseEvent(hIntr);
1.8       noro      331:        handle[0] = hIntr_Ack;
                    332:        WaitForMultipleObjects(1,(CONST HANDLE *)handle,FALSE,(DWORD)-1);
1.1       noro      333: }
                    334:
                    335: void terminate_asir() {
                    336:        PulseEvent(hKill);
                    337: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>