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

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

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