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

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:
                    125: BOOL Init_IO(char *errmsg) {
                    126:        int i;
                    127:        char *av[BUFSIZ];
                    128:        char AsirExe[BUFSIZ];
                    129:        DWORD mypid,len;
                    130:        HANDLE hR0,hW0,hR1,hW1;
                    131:     SECURITY_ATTRIBUTES SecurityAttributes;
                    132:        char remread[10],remwrite[10];
                    133:        char notify[100],notify_ack[100],intr[100],intr_ack[100],kill[100];
                    134:        char name[BUFSIZ],qname[BUFSIZ],home[BUFSIZ];
                    135:
                    136:        _setargv();
                    137:        if ( !strcmp(__argv[0],"debuggui") ) {
1.2       noro      138:                asirgui_kind = ASIRGUI_DEBUG;
1.1       noro      139:                hRead = atoi(__argv[1]);
                    140:                hWrite = atoi(__argv[2]);
                    141:                hNotify = OpenEvent(EVENT_ALL_ACCESS|EVENT_MODIFY_STATE,TRUE,__argv[3]);
                    142:                hNotify_Ack = OpenEvent(EVENT_ALL_ACCESS|EVENT_MODIFY_STATE,TRUE,__argv[4]);
                    143:                hIntr = OpenEvent(EVENT_ALL_ACCESS|EVENT_MODIFY_STATE,TRUE,__argv[5]);
                    144:                hIntr_Ack = OpenEvent(EVENT_ALL_ACCESS|EVENT_MODIFY_STATE,TRUE,__argv[6]);
                    145:                hKill = OpenEvent(EVENT_ALL_ACCESS|EVENT_MODIFY_STATE,TRUE,__argv[7]);
                    146:                return TRUE;
                    147:        } else if ( !strcmp(__argv[0],"messagegui") ) {
1.2       noro      148:                asirgui_kind = ASIRGUI_MESSAGE;
1.1       noro      149:                hRead = atoi(__argv[1]);
                    150:                hWrite = atoi(__argv[2]);
                    151:                hNotify = OpenEvent(EVENT_ALL_ACCESS|EVENT_MODIFY_STATE,TRUE,__argv[3]);
                    152:                hNotify_Ack = OpenEvent(EVENT_ALL_ACCESS|EVENT_MODIFY_STATE,TRUE,__argv[4]);
                    153:                return TRUE;
                    154:        } else {
1.2       noro      155:                asirgui_kind = ASIRGUI_MAIN;
1.1       noro      156:                mypid = GetCurrentProcessId();
                    157:                sprintf(notify,"asir_notify_%d",mypid);
                    158:                sprintf(notify_ack,"asir_notify_ack_%d",mypid);
                    159:                sprintf(intr,"asir_intr_%d",mypid);
                    160:                sprintf(intr_ack,"asir_intr_ack_%d",mypid);
                    161:                sprintf(kill,"asir_kill_%d",mypid);
                    162:                hNotify = CreateEvent(NULL,TRUE,FALSE,notify);
                    163:                hNotify_Ack = CreateEvent(NULL,TRUE,FALSE,notify_ack);
                    164:                hIntr = CreateEvent(NULL,TRUE,FALSE,intr);
                    165:                hIntr_Ack = CreateEvent(NULL,TRUE,FALSE,intr_ack);
                    166:                hKill = CreateEvent(NULL,TRUE,FALSE,kill);
                    167:
                    168:        SecurityAttributes.nLength = sizeof(SecurityAttributes);
                    169:        SecurityAttributes.lpSecurityDescriptor = NULL;
                    170:        SecurityAttributes.bInheritHandle = TRUE;
                    171:        CreatePipe(&hR0, &hW0, &SecurityAttributes, 65536);
                    172:        CreatePipe(&hR1, &hW1, &SecurityAttributes, 65536);
                    173:
                    174:                hRead = hR0;
                    175:                hWrite = hW1;
                    176:                sprintf(remread,"%d",(DWORD)hR1);
                    177:                sprintf(remwrite,"%d",(DWORD)hW0);
                    178:
                    179:                len = sizeof(name);
                    180:                if ( get_rootdir(name,len,errmsg) == FALSE ) {
                    181:                        return FALSE;
                    182:                }
1.4       noro      183:                sprintf(AsirExe,"%s\\bin\\engine.exe",name);
1.1       noro      184: //             sprintf(AsirExe,"c:\\home\\noro\\engine2000\\debug\\engine.exe");
                    185:                if ( !getenv("HOME") ) {
                    186:                        sprintf(home,"HOME=%s",name);
                    187:                        _putenv(home);
                    188:                }
                    189:                av[0] = "asir";
                    190:                av[1] = remread;
                    191:                av[2] = remwrite;
                    192:                av[3] = notify;
                    193:                av[4] = notify_ack;
                    194:                av[5] = intr;
                    195:                av[6] = intr_ack;
                    196:                av[7] = kill;
                    197:                for ( i = 1; i < __argc; i++ )
                    198:                        av[i+7] = __argv[i];
                    199:                if ( use_current_dir )  {
                    200:                        av[i+7] = "-rootdir"; i++;
                    201:                        sprintf(qname,"\"%s\"",name);
                    202:                        av[i+7] = qname; i++;
                    203:                }
                    204:                av[i+7] = NULL;
                    205:                hProc = (HANDLE)_spawnv(_P_NOWAIT,AsirExe,av);
                    206:                if ( hProc == (HANDLE)-1 ) {
                    207:                        sprintf(errmsg,"%s is not found.",AsirExe);
                    208:                        return FALSE;
                    209:                }
                    210:                return TRUE;
                    211:        }
                    212: }
                    213:
                    214: void put_line(char *buf) {
                    215:        DWORD len;
                    216:        int size = strlen(buf);
                    217:        buf[size] = '\n';
                    218:        buf[size+1] = 0;
                    219:        size++;
                    220:        WriteFile(hWrite,&size,sizeof(int),&len,NULL);
                    221:        WriteFile(hWrite,buf,size,&len,NULL);
                    222: }
                    223:
                    224: #define FROMASIR_EXIT 0
                    225: #define FROMASIR_TEXT 1
                    226: #define FROMASIR_HEAPSIZE 2
                    227: #define FROMASIR_SHOW 3
                    228: #define FROMASIR_HIDE 4
                    229: #define FROMASIR_DEBUGPROC 5
                    230:
                    231: static char insert_buf[BUFSIZ*10];
                    232: static int insert_buf_len = 0;
                    233:
                    234: int AsirHeapSize;
                    235:
                    236: void insert_to_theView(char *);
                    237: void flush_log();
                    238:
                    239: void read_and_insert()
                    240: {
                    241:        DWORD len;
                    242:        unsigned int cmdsize,cmd,size;
                    243:
                    244:        ReadFile(hRead,&cmdsize,sizeof(unsigned int),&len,NULL);
                    245:        cmd = cmdsize>>16;
                    246:        size = cmdsize&0xffff;
                    247:
                    248:        switch ( cmd ) {
                    249:                case FROMASIR_EXIT:
                    250:                        write_input_history();
                    251:                        flush_log();
                    252:                        PostMessage(NULL,WM_QUIT,0,0);
                    253:                        break;
                    254:                case FROMASIR_TEXT:
                    255:                        if ( size ) {
                    256:                                ReadFile(hRead,insert_buf,size,&len,NULL);
                    257:                                insert_buf[size] = 0;
                    258:                                insert_to_theView(insert_buf);
                    259:                        }
                    260:                        break;
                    261:                case FROMASIR_HEAPSIZE:
                    262:                        if ( size )
                    263:                                ReadFile(hRead,&AsirHeapSize,size,&len,NULL);
                    264:                        break;
                    265:                case FROMASIR_SHOW:
                    266:                        show_window(1);
                    267:                        break;
                    268:                case FROMASIR_HIDE:
                    269:                        show_window(0);
                    270:                        break;
                    271:                default:
                    272:                        if ( size )
                    273:                                ReadFile(hRead,insert_buf,size,&len,NULL);
                    274:                        break;
                    275:        }
                    276: }
                    277:
1.5       noro      278: static char history_path[BUFSIZ];
                    279: static char history_errmsg[BUFSIZ];
                    280:
1.1       noro      281: void init_input_history()
                    282: {
1.5       noro      283:        char rootdir[BUFSIZ];
                    284:        get_rootdir(rootdir,sizeof(rootdir),history_errmsg);
                    285:        sprintf(history_path,"%s\\bin\\%s",rootdir,HISTORY);
1.1       noro      286:        init_hist(MAXHIST);
                    287: }
                    288:
                    289: void write_input_history()
                    290: {
1.5       noro      291:        write_hist(history_path);
1.1       noro      292: }
                    293:
                    294: void read_input_history()
                    295: {
1.5       noro      296:        read_hist(history_path);
1.1       noro      297: }
                    298:
                    299: void send_intr() {
                    300:        PulseEvent(hIntr);
                    301: }
                    302:
                    303: void terminate_asir() {
                    304:        PulseEvent(hKill);
                    305: }

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