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

1.1     ! takayama    1: /* $OpenXM$ */
        !             2: // cl test.c user32.lib
        !             3:
        !             4: #include <windows.h>
        !             5: #include <stdlib.h>
        !             6: #include <stdio.h>
        !             7: #include <process.h>
        !             8:
        !             9: char *winname2uxname(char winname[]);
        !            10: FILE *findAsirHandler();
        !            11: int sendFileName(HWND hnd,char uname[]);
        !            12: int loadFile(HWND hnd, char *uname);
        !            13: int terminateAsir(HWND hnd);
        !            14:
        !            15: int main(int argc, char *argv[])
        !            16: {
        !            17:   HWND hnd;
        !            18:   int i,j,c,ii;
        !            19:   char *uname=NULL;
        !            20:   FILE *fp=NULL;
        !            21:   FILE *fp2=NULL;
        !            22:   char snameWin[1024];
        !            23:   char *snameUx;
        !            24:   char *s;
        !            25:   char msg[1024];
        !            26:   char cmd[1024];
        !            27:   int paste_contents=0;
        !            28:
        !            29:   /* MessageBox(NULL,TEXT("test"),TEXT("Error in cmdasir.c"),MB_OK); */
        !            30:   sprintf(snameWin,"%s\\cmdasir-%d.txt",getenv("TEMP"),getpid());
        !            31:   snameUx = winname2uxname(snameWin);
        !            32:   if ((argc > 1) && (strcmp(argv[1],"--delete-tmp")==0)) {
        !            33:     sprintf(cmd,"del %s\\cmdasir-*",getenv("TEMP"));
        !            34:     system(cmd);
        !            35:     return(0);
        !            36:   }
        !            37:   fp = findAsirHandler();
        !            38:   if (fp == NULL) {
        !            39:     fprintf(stderr,"handler file is not found.\n"); return(-1);
        !            40:   }
        !            41:   fscanf(fp,"%d",&hnd);
        !            42:   fclose(fp);
        !            43:   if ((argc > 1) && (strcmp(argv[1],"--quit")==0)) {
        !            44:     return terminateAsir(hnd);
        !            45:   }
        !            46:   printf("Handler is %d\n",hnd);
        !            47:   paste_contents=0;
        !            48:   for (ii=1; ii<argc; ii++) {
        !            49:        if (strcmp(argv[ii],"--paste-contents")==0) {
        !            50:                paste_contents=1; continue;
        !            51:        }
        !            52:     uname = NULL;
        !            53:     if (hasTemp(argv[ii])) {
        !            54:       fp = fopen(winname2uxname(argv[ii]),"r");
        !            55:       fp2 = fopen(snameUx,"w");
        !            56:       if (fp2 == NULL) {
        !            57:         sprintf(msg,"File %s to load is not found.",argv[ii]);
        !            58:         MessageBox(NULL,TEXT(msg),TEXT("Error in cmdasir.c"), MB_OK);
        !            59:         return(-1);
        !            60:       }
        !            61:       while ((c=fgetc(fp)) >= 0) fputc(c,fp2);
        !            62:        if (!paste_contents) fprintf(fp2,"\nend$\n");
        !            63:       fclose(fp2);
        !            64:       uname=snameUx;
        !            65:     }
        !            66:     printf("filename=%s\n",argv[ii]);
        !            67:     if(access(argv[ii],0)==0) {
        !            68:        if (paste_contents) return pasteFile(hnd, (uname != NULL)? uname: winname2uxname(argv[ii]));
        !            69:        else return loadFile(hnd, (uname != NULL)? uname: winname2uxname(argv[ii]));
        !            70:     }
        !            71:   }
        !            72:   /* work file is kept. Prepare a commnd to delete all the work files under temp
        !            73:    */
        !            74:   while ((c=getchar()) != EOF) {
        !            75:     if (!PostMessage(hnd,WM_CHAR,c,1)) {
        !            76:       MessageBox(NULL,TEXT("asirgui is not running."), TEXT("ERROR in cmdasir.c"),MB_OK);
        !            77:       return(-1);
        !            78:     }
        !            79:   }
        !            80:   return 0;
        !            81: }
        !            82:
        !            83: int loadFile(HWND hnd, char *uname) {
        !            84:     char *s="load(@);\n";
        !            85:     int i;
        !            86:     for (i=0; i<strlen(s); i++) {
        !            87:         if (s[i] != '@') PostMessage(hnd,WM_CHAR,s[i],1);
        !            88:         else {
        !            89:             PostMessage(hnd,WM_CHAR,'"',1);
        !            90:             sendFileName(hnd,uname);
        !            91:             if (!PostMessage(hnd,WM_CHAR,'"',1)) {
        !            92:                 MessageBox(NULL,TEXT("asirgui is not running."),TEXT("ERROR in cmdasir.c"),MB_OK);
        !            93:                 return -1;
        !            94:             }
        !            95:         }
        !            96:     }
        !            97:     return 0;
        !            98: }
        !            99:
        !           100: int terminateAsir(HWND hnd) {
        !           101:     int i;
        !           102:     char *s="quit;\n";
        !           103:     for (i=0; i<strlen(s); i++) {
        !           104:         if (!PostMessage(hnd,WM_CHAR,s[i],1)) {
        !           105:             MessageBox(NULL,TEXT("asirgui is not running."),TEXT("ERROR in cmdasir.c"),MB_OK);
        !           106:             return -1;
        !           107:         }
        !           108:     }
        !           109:     return 0;
        !           110: }
        !           111:
        !           112: char *winname2uxname(char wname[]) {
        !           113:   int i;
        !           114:   char *uname;
        !           115:   uname = (char *) malloc(strlen(wname)+1);
        !           116:   for (i=0; i<strlen(wname); i++) {
        !           117:     if (wname[i] == '\\') uname[i] = '/';
        !           118:     else uname[i] = wname[i];
        !           119:     uname[i+1] = 0;
        !           120:   }
        !           121:   return(uname);
        !           122: }
        !           123:
        !           124: int hasTemp(char *s) {
        !           125:   int i,n;
        !           126:   n=strlen(s);
        !           127:   for (i=0; i<n-3; i++) {
        !           128:     if (strncmp(&(s[i]),"Temp",4)==0) return(1);
        !           129:   }
        !           130:   return(0);
        !           131: }
        !           132:
        !           133: FILE *findAsirHandler() {
        !           134:   int i;
        !           135:   FILE *hnd = NULL;
        !           136:   char msg[1024];
        !           137:   char path1[1024];
        !           138:   char path0[1024];
        !           139:   char *path[] = { NULL, NULL, "c:/Program files/asir/bin/asirgui.hnd", "c:/Program files (x86)/asir/bin/asirgui.hnd", "c:/asir/bin/asirgui.hnd" };
        !           140:   sprintf(path0,"%s/asirgui.hnd",(char *)getenv("TEMP"));
        !           141:   path[0]=winname2uxname(path0);
        !           142:   if (getenv("ASIR_ROOTDIR") != NULL) {
        !           143:     sprintf(path1,"%s/bin/asirgui.hnd",(char *)getenv("ASIR_ROOTDIR"));
        !           144:     path[1]=winname2uxname(path1);
        !           145:   }else{
        !           146:     sprintf(path1,"%s/Desktop/asir/bin/asirgui.hnd",(char *)getenv("HOMEPATH"));
        !           147:     path[1]=winname2uxname(path1);
        !           148:   }
        !           149:   for(i=0; i<sizeof(path); i++) {
        !           150:     if(path[i]!=NULL) {
        !           151:       hnd = fopen(path[i],"r");
        !           152:       if (hnd != NULL) return(hnd);
        !           153:     }
        !           154:   }
        !           155:   sprintf(msg,"asir handler is not found.");
        !           156:   MessageBox(NULL,TEXT(msg),TEXT("ERROR in cmdasir.c"),MB_OK);
        !           157:   return NULL;
        !           158: }
        !           159: #if 0
        !           160: int sendFileName_ascii(HWND hnd,char uname[]) {
        !           161:     int j;
        !           162:     for (j=0; j<strlen(uname);j++) PostMessage(hnd,WM_CHAR,uname[j],1);
        !           163: }
        !           164: #endif
        !           165: int sendFileName(HWND hnd,char uname[]) {
        !           166:     int j;
        !           167:     int len=strlen(uname)+1;
        !           168:     HGLOBAL hMem;
        !           169:     LPTSTR pMem;
        !           170:     if (!OpenClipboard(NULL) ) return 1;
        !           171:     hMem = GlobalAlloc(GMEM_FIXED,len);
        !           172:     pMem = (LPTSTR)hMem;
        !           173:     lstrcpy(pMem,(LPCTSTR)uname);
        !           174:     EmptyClipboard();
        !           175:     SetClipboardData(CF_TEXT,hMem);
        !           176:     CloseClipboard();
        !           177:        if (!PostMessage(hnd,WM_CHAR,0x19,1)) { /* 0x19 ctrl-Y, ctrl-V 0x16 */
        !           178:       MessageBox(NULL,TEXT("asirgui is not running."),TEXT("ERROR in cmdasir.c"),MB_OK);
        !           179:          return -1;
        !           180:        }
        !           181:        return 0;
        !           182: }
        !           183:
        !           184: int pasteFile(HWND hnd, char *uname) {
        !           185:     FILE *fp;
        !           186:        char *s=NULL;
        !           187:        char *sold=NULL;
        !           188:        int size=1024;
        !           189:        int c,i;
        !           190:        fp = fopen(uname,"r");
        !           191:        if (fp == NULL) {
        !           192:                MessageBox(NULL,TEXT("File is not found: "),TEXT(uname),MB_OK);
        !           193:                return -1;
        !           194:        }
        !           195:        s = (char *)malloc(size+1);
        !           196:        i = 0;
        !           197:        while ((c=fgetc(fp)) != EOF) {
        !           198:                s[i] = c; s[i+1]=0;
        !           199:                if (i >= size-1) {
        !           200:                        sold = s;
        !           201:                        size = size*2;
        !           202:                        s = (char*)malloc(size+1);
        !           203:                        strcpy(s,sold);
        !           204:                        free(sold);
        !           205:                }
        !           206:                i++;
        !           207:        }
        !           208:        sendFileName(hnd,s);
        !           209:        free(s);
        !           210:        PostMessage(hnd,WM_CHAR,0xa,1);
        !           211: }

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