[BACK]Return to cmdasir.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / windows / post-msg-asirgui

Annotation of OpenXM_contrib2/windows/post-msg-asirgui/cmdasir.c, Revision 1.10

1.10    ! ohara       1: /* $OpenXM: OpenXM_contrib2/windows/post-msg-asirgui/cmdasir.c,v 1.9 2014/05/10 08:23:33 ohara Exp $ */
1.3       ohara       2: // cl test.c user32.lib
                      3:
                      4: #include <windows.h>
                      5: #include <stdlib.h>
                      6: #include <stdio.h>
                      7: #include <string.h>
                      8: #include <io.h>
                      9: #include <process.h>
1.8       ohara      10: #include <fcntl.h>
1.3       ohara      11:
                     12: char *winname2uxname(char winname[]);
                     13: FILE *findAsirHandler();
                     14: int sendFileName(HWND hnd,char uname[]);
                     15: int loadFile(HWND hnd, char *uname);
                     16: int terminateAsir(HWND hnd);
                     17: int hasTemp(char *s);
                     18: int pasteFile(HWND hnd, char *uname);
1.6       ohara      19: int containEnd(char s[]);
                     20: int abortAsir(HWND hnd);
1.7       ohara      21: int main(int argc, char *argv[]);
                     22:
1.8       ohara      23: FILE *open_stdio(DWORD type) {
                     24:     HANDLE hnd;
                     25:     int fd;
                     26:     if((hnd = GetStdHandle(type)) != INVALID_HANDLE_VALUE && (fd = _open_osfhandle((intptr_t)hnd, _O_TEXT)) >= 0) {
                     27:         return _fdopen(fd, "w");
                     28:     }
                     29:     return NULL;
                     30: }
                     31:
1.7       ohara      32: int WINAPI WinMain(HINSTANCE hins, HINSTANCE prev, LPSTR arg, int show) {
1.8       ohara      33:     FILE *fp;
                     34: #if (_WIN32_WINNT > 0x0500)
                     35:     if(AttachConsole(ATTACH_PARENT_PROCESS)) {
                     36:         if(fp = open_stdio(STD_OUTPUT_HANDLE)) {
                     37:             *stdout = *fp;
                     38:             setvbuf(stdout, NULL, _IONBF, 0);
                     39:         }
                     40:     }
                     41: #endif
1.7       ohara      42:     main(__argc, __argv);
                     43:     return 0;
                     44: }
1.3       ohara      45:
                     46: int main(int argc, char *argv[])
                     47: {
                     48:   HWND hnd;
                     49:   int c,ii;
                     50:   char *uname=NULL;
                     51:   FILE *fp=NULL;
                     52:   FILE *fp2=NULL;
                     53:   char snameWin[1024];
                     54:   char *snameUx;
                     55:   char msg[1024];
                     56:   char cmd[1024];
                     57:   int paste_contents=0;
1.5       takayama   58:   int abort=0;
1.3       ohara      59:
1.10    ! ohara      60:   /* MessageBox(NULL,TEXT("test"),TEXT("ERROR in cmdasir.c"),MB_OK); */
1.8       ohara      61:   sprintf(snameWin,"%s\\cmdasir-%d.txt",getenv("TEMP"),_getpid());
1.3       ohara      62:   snameUx = winname2uxname(snameWin);
                     63:   if ((argc > 1) && (strcmp(argv[1],"--delete-tmp")==0)) {
                     64:     sprintf(cmd,"del %s\\cmdasir-*",getenv("TEMP"));
                     65:     system(cmd);
                     66:     return(0);
                     67:   }
1.5       takayama   68:
1.3       ohara      69:   fp = findAsirHandler();
                     70:   if (fp == NULL) {
1.8       ohara      71:     printf("handler file is not found.\n"); return(-1);
1.3       ohara      72:   }
                     73:   fscanf(fp,"%d",&hnd);
                     74:   fclose(fp);
                     75:   if ((argc > 1) && (strcmp(argv[1],"--quit")==0)) {
                     76:     return terminateAsir(hnd);
                     77:   }
1.5       takayama   78:   if ((argc > 1) && (strcmp(argv[1],"--abort")==0)) {
                     79:     return abortAsir(hnd);
                     80:   }
1.3       ohara      81:   printf("Handler is %d\n",hnd);
                     82:   paste_contents=0;
                     83:   for (ii=1; ii<argc; ii++) {
1.5       takayama   84:     if (strcmp(argv[ii],"--paste-contents")==0) {
                     85:       paste_contents=1; continue;
                     86:     }
1.3       ohara      87:     uname = NULL;
                     88:     if (hasTemp(argv[ii])) {
                     89:       fp = fopen(winname2uxname(argv[ii]),"r");
                     90:       fp2 = fopen(snameUx,"w");
                     91:       if (fp2 == NULL) {
                     92:         sprintf(msg,"File %s to load is not found.",argv[ii]);
1.10    ! ohara      93:         MessageBox(NULL,TEXT(msg),TEXT("ERROR in cmdasir.c"), MB_OK);
1.3       ohara      94:         return(-1);
                     95:       }
                     96:       while ((c=fgetc(fp)) >= 0) fputc(c,fp2);
                     97:        if (!paste_contents) fprintf(fp2,"\nend$\n");
                     98:       fclose(fp2);
                     99:       uname=snameUx;
                    100:     }
                    101:     printf("filename=%s\n",argv[ii]);
1.8       ohara     102:     if(_access(argv[ii],0)==0) {
1.3       ohara     103:        if (paste_contents) return pasteFile(hnd, (uname != NULL)? uname: winname2uxname(argv[ii]));
                    104:        else return loadFile(hnd, (uname != NULL)? uname: winname2uxname(argv[ii]));
                    105:     }
                    106:   }
                    107:   /* work file is kept. Prepare a commnd to delete all the work files under temp
                    108:    */
                    109:   while ((c=getchar()) != EOF) {
                    110:     if (!PostMessage(hnd,WM_CHAR,c,1)) {
                    111:       MessageBox(NULL,TEXT("asirgui is not running."), TEXT("ERROR in cmdasir.c"),MB_OK);
                    112:       return(-1);
                    113:     }
                    114:   }
                    115:   return 0;
                    116: }
                    117:
                    118: int loadFile(HWND hnd, char *uname) {
                    119:     char *s="load(@);\n";
                    120:     int i;
                    121:     for (i=0; i<strlen(s); i++) {
                    122:         if (s[i] != '@') PostMessage(hnd,WM_CHAR,s[i],1);
                    123:         else {
                    124:             PostMessage(hnd,WM_CHAR,'"',1);
                    125:             sendFileName(hnd,uname);
                    126:             if (!PostMessage(hnd,WM_CHAR,'"',1)) {
                    127:                 MessageBox(NULL,TEXT("asirgui is not running."),TEXT("ERROR in cmdasir.c"),MB_OK);
                    128:                 return -1;
                    129:             }
                    130:         }
                    131:     }
                    132:     return 0;
                    133: }
                    134:
                    135: int terminateAsir(HWND hnd) {
                    136:     int i;
                    137:     char *s="quit;\n";
                    138:     for (i=0; i<strlen(s); i++) {
                    139:         if (!PostMessage(hnd,WM_CHAR,s[i],1)) {
                    140:             MessageBox(NULL,TEXT("asirgui is not running."),TEXT("ERROR in cmdasir.c"),MB_OK);
                    141:             return -1;
                    142:         }
                    143:     }
                    144:     return 0;
                    145: }
                    146:
                    147: char *winname2uxname(char wname[]) {
                    148:   int i;
                    149:   char *uname;
                    150:   uname = (char *) malloc(strlen(wname)+1);
                    151:   for (i=0; i<strlen(wname); i++) {
                    152:     if (wname[i] == '\\') uname[i] = '/';
                    153:     else uname[i] = wname[i];
                    154:     uname[i+1] = 0;
                    155:   }
                    156:   return(uname);
                    157: }
                    158:
                    159: #if 0
                    160: int hasTemp(char *s) {
                    161:   int i;
                    162:   size_t n=strlen(s);
                    163:   for (i=0; i<n-3; i++) {
                    164:     if (strncmp(&(s[i]),"Temp",4)==0) return(1);
                    165:   }
                    166:   return(0);
                    167: }
                    168: #endif
                    169:
                    170: int hasTemp(char *s) {
                    171:        return strstr(s, "Temp")!=NULL;
                    172: }
                    173:
                    174: FILE *findAsirHandler() {
                    175:   int i;
                    176:   FILE *hnd = NULL;
                    177:   char msg[1024];
                    178:   char path1[1024];
                    179:   char path0[1024];
                    180:   char *path[] = { NULL, NULL, "c:/Program files/asir/bin/asirgui.hnd", "c:/Program files (x86)/asir/bin/asirgui.hnd", "c:/asir/bin/asirgui.hnd" };
1.9       ohara     181:   sprintf(path0,"%s/asirgui.hnd",getenv("TEMP"));
1.3       ohara     182:   path[0]=winname2uxname(path0);
                    183:   if (getenv("ASIR_ROOTDIR") != NULL) {
1.9       ohara     184:     sprintf(path1,"%s/bin/asirgui.hnd",getenv("ASIR_ROOTDIR"));
1.3       ohara     185:     path[1]=winname2uxname(path1);
                    186:   }else{
1.9       ohara     187:     sprintf(path1,"%s%s/Desktop/asir/bin/asirgui.hnd",getenv("HOMEDRIVE"),getenv("HOMEPATH"));
1.3       ohara     188:     path[1]=winname2uxname(path1);
                    189:   }
                    190:   for(i=0; i<sizeof(path); i++) {
                    191:     if(path[i]!=NULL) {
                    192:       hnd = fopen(path[i],"r");
                    193:       if (hnd != NULL) return(hnd);
                    194:     }
                    195:   }
                    196:   sprintf(msg,"asir handler is not found.");
                    197:   MessageBox(NULL,TEXT(msg),TEXT("ERROR in cmdasir.c"),MB_OK);
                    198:   return NULL;
                    199: }
                    200: #if 0
                    201: int sendFileName_ascii(HWND hnd,char uname[]) {
                    202:     int j;
                    203:     for (j=0; j<strlen(uname);j++) PostMessage(hnd,WM_CHAR,uname[j],1);
                    204: }
                    205: #endif
                    206: int sendFileName(HWND hnd,char uname[]) {
                    207:     size_t len=strlen(uname)+1;
                    208:     HGLOBAL hMem;
                    209:     LPTSTR pMem;
                    210:     if (!OpenClipboard(NULL) ) return 1;
                    211:     hMem = GlobalAlloc(GMEM_FIXED,len);
                    212:     pMem = (LPTSTR)hMem;
                    213:     lstrcpy(pMem,(LPCTSTR)uname);
                    214:     EmptyClipboard();
                    215:     SetClipboardData(CF_TEXT,hMem);
                    216:     CloseClipboard();
                    217:        if (!PostMessage(hnd,WM_CHAR,0x19,1)) { /* 0x19 ctrl-Y, ctrl-V 0x16 */
                    218:       MessageBox(NULL,TEXT("asirgui is not running."),TEXT("ERROR in cmdasir.c"),MB_OK);
                    219:          return -1;
                    220:        }
                    221:        return 0;
                    222: }
                    223:
                    224: int pasteFile(HWND hnd, char *uname) {
                    225:     FILE *fp;
                    226:        char *s=NULL;
                    227:        char *sold=NULL;
                    228:        int size=1024;
                    229:        int c,i;
                    230:        fp = fopen(uname,"r");
                    231:        if (fp == NULL) {
                    232:                MessageBox(NULL,TEXT("File is not found: "),TEXT(uname),MB_OK);
                    233:                return -1;
                    234:        }
                    235:        s = (char *)malloc(size+1);
1.4       takayama  236:        i = 0;s[0]=0;
1.3       ohara     237:        while ((c=fgetc(fp)) != EOF) {
                    238:                s[i] = c; s[i+1]=0;
                    239:                if (i >= size-1) {
                    240:                        sold = s;
                    241:                        size = size*2;
                    242:                        s = (char*)malloc(size+1);
                    243:                        strcpy(s,sold);
                    244:                        free(sold);
                    245:                }
                    246:                i++;
                    247:        }
1.4       takayama  248:        if (containEnd(s)) {
                    249:                MessageBox(NULL,TEXT("The string contains end$ or end;"),TEXT(uname),MB_OK);
                    250:                return(0);
                    251:        }
                    252:        if (strlen(s)>0) sendFileName(hnd,s); else {
                    253:                MessageBox(NULL,TEXT("Empty string: "),TEXT(uname),MB_OK);
                    254:                return 0;
                    255:        }
1.3       ohara     256:        free(s);
                    257:        PostMessage(hnd,WM_CHAR,0xa,1);
                    258:        return 0;
                    259: }
1.4       takayama  260:
1.5       takayama  261: int containEnd(char s[]) {
1.4       takayama  262:        int i;
                    263:        for (i=0; i<strlen(s)-4; i++) {
                    264:                if (strncmp(&(s[i]),"end$",4)==0) return 1;
                    265:                if (strncmp(&(s[i]),"end;",4)==0) return 1;
                    266:        }
                    267:        return(0);
                    268: }
1.5       takayama  269:
1.6       ohara     270: int abortAsir(HWND hnd) {
1.5       takayama  271:   int c;
                    272:   int result;
                    273:   result=MessageBox(NULL,TEXT("Do you abort this computation?"),TEXT("Abort"),MB_OKCANCEL);
                    274:   if (result == IDCANCEL) return(0);
                    275:   c=0x3;
                    276:   if (!PostMessage(hnd,WM_CHAR,c,1)) {
                    277:     MessageBox(NULL,TEXT("asirgui is not running."), TEXT("ERROR in cmdasir.c"),MB_OK);
                    278:     return(-1);
                    279:   }
                    280:
                    281:   printf("Sending ctrl-C\n");
                    282:   /* Sleep(3*1000); */
                    283:   result=MessageBox(NULL,TEXT("Interrupt is displayed in asirgui?"),TEXT("Sent ctrl-C."),MB_OKCANCEL);
                    284:   if (result == IDCANCEL) return(0);
                    285:   c='t'; PostMessage(hnd,WM_CHAR,c,1);
                    286:   c=0xd; PostMessage(hnd,WM_CHAR,c,1);
                    287:   c='y'; PostMessage(hnd,WM_CHAR,c,1);
                    288:   c=0xd; PostMessage(hnd,WM_CHAR,c,1);
                    289:   return(0);
                    290: }

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