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

Diff for /OpenXM_contrib2/windows/post-msg-asirgui/cmdasir.c between version 1.2 and 1.10

version 1.2, 2013/08/27 07:41:22 version 1.10, 2014/05/25 21:01:04
Line 1 
Line 1 
 /* $OpenXM: OpenXM_contrib2/windows/post-msg-asirgui/cmdasir.c,v 1.1 2013/08/27 05:51:50 takayama Exp $ */  /* $OpenXM: OpenXM_contrib2/windows/post-msg-asirgui/cmdasir.c,v 1.9 2014/05/10 08:23:33 ohara Exp $ */
 // cl test.c user32.lib  // cl test.c user32.lib
   
 #include <windows.h>  #include <windows.h>
 #include <stdlib.h>  #include <stdlib.h>
 #include <stdio.h>  #include <stdio.h>
 #include <string.h>  #include <string.h>
 #include <io.h>  #include <io.h>
 #include <process.h>  #include <process.h>
   #include <fcntl.h>
 char *winname2uxname(char winname[]);  
 FILE *findAsirHandler();  char *winname2uxname(char winname[]);
 int sendFileName(HWND hnd,char uname[]);  FILE *findAsirHandler();
 int loadFile(HWND hnd, char *uname);  int sendFileName(HWND hnd,char uname[]);
 int terminateAsir(HWND hnd);  int loadFile(HWND hnd, char *uname);
 int hasTemp(char *s);  int terminateAsir(HWND hnd);
 int pasteFile(HWND hnd, char *uname);  int hasTemp(char *s);
   int pasteFile(HWND hnd, char *uname);
 int main(int argc, char *argv[])  int containEnd(char s[]);
 {  int abortAsir(HWND hnd);
   HWND hnd;  int main(int argc, char *argv[]);
   int c,ii;  
   char *uname=NULL;  FILE *open_stdio(DWORD type) {
   FILE *fp=NULL;      HANDLE hnd;
   FILE *fp2=NULL;      int fd;
   char snameWin[1024];      if((hnd = GetStdHandle(type)) != INVALID_HANDLE_VALUE && (fd = _open_osfhandle((intptr_t)hnd, _O_TEXT)) >= 0) {
   char *snameUx;          return _fdopen(fd, "w");
   char msg[1024];      }
   char cmd[1024];      return NULL;
   int paste_contents=0;  }
   
   /* MessageBox(NULL,TEXT("test"),TEXT("Error in cmdasir.c"),MB_OK); */  int WINAPI WinMain(HINSTANCE hins, HINSTANCE prev, LPSTR arg, int show) {
   sprintf(snameWin,"%s\\cmdasir-%d.txt",getenv("TEMP"),getpid());      FILE *fp;
   snameUx = winname2uxname(snameWin);  #if (_WIN32_WINNT > 0x0500)
   if ((argc > 1) && (strcmp(argv[1],"--delete-tmp")==0)) {      if(AttachConsole(ATTACH_PARENT_PROCESS)) {
     sprintf(cmd,"del %s\\cmdasir-*",getenv("TEMP"));          if(fp = open_stdio(STD_OUTPUT_HANDLE)) {
     system(cmd);              *stdout = *fp;
     return(0);              setvbuf(stdout, NULL, _IONBF, 0);
   }          }
   fp = findAsirHandler();      }
   if (fp == NULL) {  #endif
     fprintf(stderr,"handler file is not found.\n"); return(-1);      main(__argc, __argv);
   }      return 0;
   fscanf(fp,"%d",&hnd);  }
   fclose(fp);  
   if ((argc > 1) && (strcmp(argv[1],"--quit")==0)) {  int main(int argc, char *argv[])
     return terminateAsir(hnd);  {
   }    HWND hnd;
   printf("Handler is %d\n",hnd);    int c,ii;
   paste_contents=0;    char *uname=NULL;
   for (ii=1; ii<argc; ii++) {    FILE *fp=NULL;
         if (strcmp(argv[ii],"--paste-contents")==0) {    FILE *fp2=NULL;
                 paste_contents=1; continue;    char snameWin[1024];
         }    char *snameUx;
     uname = NULL;    char msg[1024];
     if (hasTemp(argv[ii])) {    char cmd[1024];
       fp = fopen(winname2uxname(argv[ii]),"r");    int paste_contents=0;
       fp2 = fopen(snameUx,"w");    int abort=0;
       if (fp2 == NULL) {  
         sprintf(msg,"File %s to load is not found.",argv[ii]);    /* MessageBox(NULL,TEXT("test"),TEXT("ERROR in cmdasir.c"),MB_OK); */
         MessageBox(NULL,TEXT(msg),TEXT("Error in cmdasir.c"), MB_OK);    sprintf(snameWin,"%s\\cmdasir-%d.txt",getenv("TEMP"),_getpid());
         return(-1);    snameUx = winname2uxname(snameWin);
       }    if ((argc > 1) && (strcmp(argv[1],"--delete-tmp")==0)) {
       while ((c=fgetc(fp)) >= 0) fputc(c,fp2);      sprintf(cmd,"del %s\\cmdasir-*",getenv("TEMP"));
         if (!paste_contents) fprintf(fp2,"\nend$\n");      system(cmd);
       fclose(fp2);      return(0);
       uname=snameUx;    }
     }  
     printf("filename=%s\n",argv[ii]);    fp = findAsirHandler();
     if(access(argv[ii],0)==0) {    if (fp == NULL) {
         if (paste_contents) return pasteFile(hnd, (uname != NULL)? uname: winname2uxname(argv[ii]));      printf("handler file is not found.\n"); return(-1);
         else return loadFile(hnd, (uname != NULL)? uname: winname2uxname(argv[ii]));    }
     }    fscanf(fp,"%d",&hnd);
   }    fclose(fp);
   /* work file is kept. Prepare a commnd to delete all the work files under temp    if ((argc > 1) && (strcmp(argv[1],"--quit")==0)) {
    */      return terminateAsir(hnd);
   while ((c=getchar()) != EOF) {    }
     if (!PostMessage(hnd,WM_CHAR,c,1)) {    if ((argc > 1) && (strcmp(argv[1],"--abort")==0)) {
       MessageBox(NULL,TEXT("asirgui is not running."), TEXT("ERROR in cmdasir.c"),MB_OK);      return abortAsir(hnd);
       return(-1);    }
     }    printf("Handler is %d\n",hnd);
   }    paste_contents=0;
   return 0;    for (ii=1; ii<argc; ii++) {
 }      if (strcmp(argv[ii],"--paste-contents")==0) {
         paste_contents=1; continue;
 int loadFile(HWND hnd, char *uname) {      }
     char *s="load(@);\n";      uname = NULL;
     int i;      if (hasTemp(argv[ii])) {
     for (i=0; i<strlen(s); i++) {        fp = fopen(winname2uxname(argv[ii]),"r");
         if (s[i] != '@') PostMessage(hnd,WM_CHAR,s[i],1);        fp2 = fopen(snameUx,"w");
         else {        if (fp2 == NULL) {
             PostMessage(hnd,WM_CHAR,'"',1);          sprintf(msg,"File %s to load is not found.",argv[ii]);
             sendFileName(hnd,uname);          MessageBox(NULL,TEXT(msg),TEXT("ERROR in cmdasir.c"), MB_OK);
             if (!PostMessage(hnd,WM_CHAR,'"',1)) {          return(-1);
                 MessageBox(NULL,TEXT("asirgui is not running."),TEXT("ERROR in cmdasir.c"),MB_OK);        }
                 return -1;        while ((c=fgetc(fp)) >= 0) fputc(c,fp2);
             }          if (!paste_contents) fprintf(fp2,"\nend$\n");
         }        fclose(fp2);
     }        uname=snameUx;
     return 0;      }
 }      printf("filename=%s\n",argv[ii]);
       if(_access(argv[ii],0)==0) {
 int terminateAsir(HWND hnd) {          if (paste_contents) return pasteFile(hnd, (uname != NULL)? uname: winname2uxname(argv[ii]));
     int i;          else return loadFile(hnd, (uname != NULL)? uname: winname2uxname(argv[ii]));
     char *s="quit;\n";      }
     for (i=0; i<strlen(s); i++) {    }
         if (!PostMessage(hnd,WM_CHAR,s[i],1)) {    /* work file is kept. Prepare a commnd to delete all the work files under temp
             MessageBox(NULL,TEXT("asirgui is not running."),TEXT("ERROR in cmdasir.c"),MB_OK);     */
             return -1;    while ((c=getchar()) != EOF) {
         }      if (!PostMessage(hnd,WM_CHAR,c,1)) {
     }        MessageBox(NULL,TEXT("asirgui is not running."), TEXT("ERROR in cmdasir.c"),MB_OK);
     return 0;        return(-1);
 }      }
     }
 char *winname2uxname(char wname[]) {    return 0;
   int i;  }
   char *uname;  
   uname = (char *) malloc(strlen(wname)+1);  int loadFile(HWND hnd, char *uname) {
   for (i=0; i<strlen(wname); i++) {      char *s="load(@);\n";
     if (wname[i] == '\\') uname[i] = '/';      int i;
     else uname[i] = wname[i];      for (i=0; i<strlen(s); i++) {
     uname[i+1] = 0;          if (s[i] != '@') PostMessage(hnd,WM_CHAR,s[i],1);
   }          else {
   return(uname);              PostMessage(hnd,WM_CHAR,'"',1);
 }              sendFileName(hnd,uname);
               if (!PostMessage(hnd,WM_CHAR,'"',1)) {
 #if 0                  MessageBox(NULL,TEXT("asirgui is not running."),TEXT("ERROR in cmdasir.c"),MB_OK);
 int hasTemp(char *s) {                  return -1;
   int i;              }
   size_t n=strlen(s);          }
   for (i=0; i<n-3; i++) {      }
     if (strncmp(&(s[i]),"Temp",4)==0) return(1);      return 0;
   }  }
   return(0);  
 }  int terminateAsir(HWND hnd) {
 #endif      int i;
       char *s="quit;\n";
 int hasTemp(char *s) {      for (i=0; i<strlen(s); i++) {
         return strstr(s, "Temp")!=NULL;          if (!PostMessage(hnd,WM_CHAR,s[i],1)) {
 }              MessageBox(NULL,TEXT("asirgui is not running."),TEXT("ERROR in cmdasir.c"),MB_OK);
               return -1;
 FILE *findAsirHandler() {          }
   int i;      }
   FILE *hnd = NULL;      return 0;
   char msg[1024];  }
   char path1[1024];  
   char path0[1024];  char *winname2uxname(char wname[]) {
   char *path[] = { NULL, NULL, "c:/Program files/asir/bin/asirgui.hnd", "c:/Program files (x86)/asir/bin/asirgui.hnd", "c:/asir/bin/asirgui.hnd" };    int i;
   sprintf(path0,"%s/asirgui.hnd",(char *)getenv("TEMP"));    char *uname;
   path[0]=winname2uxname(path0);    uname = (char *) malloc(strlen(wname)+1);
   if (getenv("ASIR_ROOTDIR") != NULL) {    for (i=0; i<strlen(wname); i++) {
     sprintf(path1,"%s/bin/asirgui.hnd",(char *)getenv("ASIR_ROOTDIR"));      if (wname[i] == '\\') uname[i] = '/';
     path[1]=winname2uxname(path1);      else uname[i] = wname[i];
   }else{      uname[i+1] = 0;
     sprintf(path1,"%s/Desktop/asir/bin/asirgui.hnd",(char *)getenv("HOMEPATH"));    }
     path[1]=winname2uxname(path1);    return(uname);
   }  }
   for(i=0; i<sizeof(path); i++) {  
     if(path[i]!=NULL) {  #if 0
       hnd = fopen(path[i],"r");  int hasTemp(char *s) {
       if (hnd != NULL) return(hnd);    int i;
     }    size_t n=strlen(s);
   }    for (i=0; i<n-3; i++) {
   sprintf(msg,"asir handler is not found.");      if (strncmp(&(s[i]),"Temp",4)==0) return(1);
   MessageBox(NULL,TEXT(msg),TEXT("ERROR in cmdasir.c"),MB_OK);    }
   return NULL;    return(0);
 }  }
 #if 0  #endif
 int sendFileName_ascii(HWND hnd,char uname[]) {  
     int j;  int hasTemp(char *s) {
     for (j=0; j<strlen(uname);j++) PostMessage(hnd,WM_CHAR,uname[j],1);          return strstr(s, "Temp")!=NULL;
 }  }
 #endif  
 int sendFileName(HWND hnd,char uname[]) {  FILE *findAsirHandler() {
     size_t len=strlen(uname)+1;    int i;
     HGLOBAL hMem;    FILE *hnd = NULL;
     LPTSTR pMem;    char msg[1024];
     if (!OpenClipboard(NULL) ) return 1;    char path1[1024];
     hMem = GlobalAlloc(GMEM_FIXED,len);    char path0[1024];
     pMem = (LPTSTR)hMem;    char *path[] = { NULL, NULL, "c:/Program files/asir/bin/asirgui.hnd", "c:/Program files (x86)/asir/bin/asirgui.hnd", "c:/asir/bin/asirgui.hnd" };
     lstrcpy(pMem,(LPCTSTR)uname);    sprintf(path0,"%s/asirgui.hnd",getenv("TEMP"));
     EmptyClipboard();    path[0]=winname2uxname(path0);
     SetClipboardData(CF_TEXT,hMem);    if (getenv("ASIR_ROOTDIR") != NULL) {
     CloseClipboard();      sprintf(path1,"%s/bin/asirgui.hnd",getenv("ASIR_ROOTDIR"));
         if (!PostMessage(hnd,WM_CHAR,0x19,1)) { /* 0x19 ctrl-Y, ctrl-V 0x16 */      path[1]=winname2uxname(path1);
       MessageBox(NULL,TEXT("asirgui is not running."),TEXT("ERROR in cmdasir.c"),MB_OK);    }else{
           return -1;      sprintf(path1,"%s%s/Desktop/asir/bin/asirgui.hnd",getenv("HOMEDRIVE"),getenv("HOMEPATH"));
         }      path[1]=winname2uxname(path1);
         return 0;    }
 }    for(i=0; i<sizeof(path); i++) {
       if(path[i]!=NULL) {
 int pasteFile(HWND hnd, char *uname) {        hnd = fopen(path[i],"r");
     FILE *fp;        if (hnd != NULL) return(hnd);
         char *s=NULL;      }
         char *sold=NULL;    }
         int size=1024;    sprintf(msg,"asir handler is not found.");
         int c,i;    MessageBox(NULL,TEXT(msg),TEXT("ERROR in cmdasir.c"),MB_OK);
         fp = fopen(uname,"r");    return NULL;
         if (fp == NULL) {  }
                 MessageBox(NULL,TEXT("File is not found: "),TEXT(uname),MB_OK);  #if 0
                 return -1;  int sendFileName_ascii(HWND hnd,char uname[]) {
         }      int j;
         s = (char *)malloc(size+1);      for (j=0; j<strlen(uname);j++) PostMessage(hnd,WM_CHAR,uname[j],1);
         i = 0;  }
         while ((c=fgetc(fp)) != EOF) {  #endif
                 s[i] = c; s[i+1]=0;  int sendFileName(HWND hnd,char uname[]) {
                 if (i >= size-1) {      size_t len=strlen(uname)+1;
                         sold = s;      HGLOBAL hMem;
                         size = size*2;      LPTSTR pMem;
                         s = (char*)malloc(size+1);      if (!OpenClipboard(NULL) ) return 1;
                         strcpy(s,sold);      hMem = GlobalAlloc(GMEM_FIXED,len);
                         free(sold);      pMem = (LPTSTR)hMem;
                 }      lstrcpy(pMem,(LPCTSTR)uname);
                 i++;      EmptyClipboard();
         }      SetClipboardData(CF_TEXT,hMem);
         sendFileName(hnd,s);      CloseClipboard();
         free(s);          if (!PostMessage(hnd,WM_CHAR,0x19,1)) { /* 0x19 ctrl-Y, ctrl-V 0x16 */
         PostMessage(hnd,WM_CHAR,0xa,1);        MessageBox(NULL,TEXT("asirgui is not running."),TEXT("ERROR in cmdasir.c"),MB_OK);
         return 0;            return -1;
 }          }
           return 0;
   }
   
   int pasteFile(HWND hnd, char *uname) {
       FILE *fp;
           char *s=NULL;
           char *sold=NULL;
           int size=1024;
           int c,i;
           fp = fopen(uname,"r");
           if (fp == NULL) {
                   MessageBox(NULL,TEXT("File is not found: "),TEXT(uname),MB_OK);
                   return -1;
           }
           s = (char *)malloc(size+1);
           i = 0;s[0]=0;
           while ((c=fgetc(fp)) != EOF) {
                   s[i] = c; s[i+1]=0;
                   if (i >= size-1) {
                           sold = s;
                           size = size*2;
                           s = (char*)malloc(size+1);
                           strcpy(s,sold);
                           free(sold);
                   }
                   i++;
           }
           if (containEnd(s)) {
                   MessageBox(NULL,TEXT("The string contains end$ or end;"),TEXT(uname),MB_OK);
                   return(0);
           }
           if (strlen(s)>0) sendFileName(hnd,s); else {
                   MessageBox(NULL,TEXT("Empty string: "),TEXT(uname),MB_OK);
                   return 0;
           }
           free(s);
           PostMessage(hnd,WM_CHAR,0xa,1);
           return 0;
   }
   
   int containEnd(char s[]) {
           int i;
           for (i=0; i<strlen(s)-4; i++) {
                   if (strncmp(&(s[i]),"end$",4)==0) return 1;
                   if (strncmp(&(s[i]),"end;",4)==0) return 1;
           }
           return(0);
   }
   
   int abortAsir(HWND hnd) {
     int c;
     int result;
     result=MessageBox(NULL,TEXT("Do you abort this computation?"),TEXT("Abort"),MB_OKCANCEL);
     if (result == IDCANCEL) return(0);
     c=0x3;
     if (!PostMessage(hnd,WM_CHAR,c,1)) {
       MessageBox(NULL,TEXT("asirgui is not running."), TEXT("ERROR in cmdasir.c"),MB_OK);
       return(-1);
     }
   
     printf("Sending ctrl-C\n");
     /* Sleep(3*1000); */
     result=MessageBox(NULL,TEXT("Interrupt is displayed in asirgui?"),TEXT("Sent ctrl-C."),MB_OKCANCEL);
     if (result == IDCANCEL) return(0);
     c='t'; PostMessage(hnd,WM_CHAR,c,1);
     c=0xd; PostMessage(hnd,WM_CHAR,c,1);
     c='y'; PostMessage(hnd,WM_CHAR,c,1);
     c=0xd; PostMessage(hnd,WM_CHAR,c,1);
     return(0);
   }

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.10

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