[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.13

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

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