[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.3 and 1.11

version 1.3, 2013/08/28 05:33:02 version 1.11, 2014/05/28 02:46:55
Line 1 
Line 1 
 /* $OpenXM: OpenXM_contrib2/windows/post-msg-asirgui/cmdasir.c,v 1.2 2013/08/27 07:41:22 ohara Exp $ */  /* $OpenXM: OpenXM_contrib2/windows/post-msg-asirgui/cmdasir.c,v 1.10 2014/05/25 21:01:04 ohara Exp $ */
 // cl test.c user32.lib  // cl test.c user32.lib
   
 #include <windows.h>  #include <windows.h>
Line 7 
Line 7 
 #include <string.h>  #include <string.h>
 #include <io.h>  #include <io.h>
 #include <process.h>  #include <process.h>
   #include <fcntl.h>
   #include <winnls.h>
   
 char *winname2uxname(char winname[]);  char *winname2uxname(char winname[]);
 FILE *findAsirHandler();  FILE *findAsirHandler();
Line 15  int loadFile(HWND hnd, char *uname);
Line 17  int loadFile(HWND hnd, char *uname);
 int terminateAsir(HWND hnd);  int terminateAsir(HWND hnd);
 int hasTemp(char *s);  int hasTemp(char *s);
 int pasteFile(HWND hnd, char *uname);  int pasteFile(HWND hnd, char *uname);
   int containEnd(char s[]);
   int abortAsir(HWND hnd);
   int main(int argc, char *argv[]);
   int damemsg(unsigned char s[]);
   int damemoji(unsigned char s[]);
   
   FILE *open_stdio(DWORD type) {
       HANDLE hnd;
       int fd;
       if((hnd = GetStdHandle(type)) != INVALID_HANDLE_VALUE && (fd = _open_osfhandle((intptr_t)hnd, _O_TEXT)) >= 0) {
           return _fdopen(fd, "w");
       }
       return NULL;
   }
   
   int WINAPI WinMain(HINSTANCE hins, HINSTANCE prev, LPSTR arg, int show) {
       FILE *fp;
   #if (_WIN32_WINNT > 0x0500)
       if(AttachConsole(ATTACH_PARENT_PROCESS)) {
           if(fp = open_stdio(STD_OUTPUT_HANDLE)) {
               *stdout = *fp;
               setvbuf(stdout, NULL, _IONBF, 0);
           }
       }
   #endif
       main(__argc, __argv);
       return 0;
   }
   
 int main(int argc, char *argv[])  int main(int argc, char *argv[])
 {  {
   HWND hnd;    HWND hnd;
Line 28  int main(int argc, char *argv[])
Line 58  int main(int argc, char *argv[])
   char msg[1024];    char msg[1024];
   char cmd[1024];    char cmd[1024];
   int paste_contents=0;    int paste_contents=0;
     int abort=0;
   
   /* MessageBox(NULL,TEXT("test"),TEXT("Error in cmdasir.c"),MB_OK); */    /* MessageBox(NULL,TEXT("test"),TEXT("ERROR in cmdasir.c"),MB_OK); */
   sprintf(snameWin,"%s\\cmdasir-%d.txt",getenv("TEMP"),getpid());    sprintf(snameWin,"%s\\cmdasir-%d.txt",getenv("TEMP"),_getpid());
   snameUx = winname2uxname(snameWin);    snameUx = winname2uxname(snameWin);
   if ((argc > 1) && (strcmp(argv[1],"--delete-tmp")==0)) {    if ((argc > 1) && (strcmp(argv[1],"--delete-tmp")==0)) {
     sprintf(cmd,"del %s\\cmdasir-*",getenv("TEMP"));      sprintf(cmd,"del %s\\cmdasir-*",getenv("TEMP"));
     system(cmd);      system(cmd);
     return(0);      return(0);
   }    }
   
   fp = findAsirHandler();    fp = findAsirHandler();
   if (fp == NULL) {    if (fp == NULL) {
     fprintf(stderr,"handler file is not found.\n"); return(-1);      printf("handler file is not found.\n"); return(-1);
   }    }
   fscanf(fp,"%d",&hnd);    fscanf(fp,"%d",&hnd);
   fclose(fp);    fclose(fp);
   if ((argc > 1) && (strcmp(argv[1],"--quit")==0)) {    if ((argc > 1) && (strcmp(argv[1],"--quit")==0)) {
     return terminateAsir(hnd);      return terminateAsir(hnd);
   }    }
     if ((argc > 1) && (strcmp(argv[1],"--abort")==0)) {
       return abortAsir(hnd);
     }
   printf("Handler is %d\n",hnd);    printf("Handler is %d\n",hnd);
   paste_contents=0;    paste_contents=0;
   for (ii=1; ii<argc; ii++) {    for (ii=1; ii<argc; ii++) {
         if (strcmp(argv[ii],"--paste-contents")==0) {      if (strcmp(argv[ii],"--paste-contents")==0) {
                 paste_contents=1; continue;        paste_contents=1; continue;
         }      }
     uname = NULL;      uname = NULL;
     if (hasTemp(argv[ii])) {      if (hasTemp(argv[ii])) {
       fp = fopen(winname2uxname(argv[ii]),"r");        fp = fopen(winname2uxname(argv[ii]),"r");
       fp2 = fopen(snameUx,"w");        fp2 = fopen(snameUx,"w");
       if (fp2 == NULL) {        if (fp2 == NULL) {
         sprintf(msg,"File %s to load is not found.",argv[ii]);          sprintf(msg,"File %s to load is not found.",argv[ii]);
         MessageBox(NULL,TEXT(msg),TEXT("Error in cmdasir.c"), MB_OK);          MessageBox(NULL,TEXT(msg),TEXT("ERROR in cmdasir.c"), MB_OK);
         return(-1);          return(-1);
       }        }
       while ((c=fgetc(fp)) >= 0) fputc(c,fp2);        while ((c=fgetc(fp)) >= 0) fputc(c,fp2);
Line 67  int main(int argc, char *argv[])
Line 102  int main(int argc, char *argv[])
       uname=snameUx;        uname=snameUx;
     }      }
     printf("filename=%s\n",argv[ii]);      printf("filename=%s\n",argv[ii]);
     if(access(argv[ii],0)==0) {      if(_access(argv[ii],0)==0) {
         if (paste_contents) return pasteFile(hnd, (uname != NULL)? uname: winname2uxname(argv[ii]));          if (paste_contents) return pasteFile(hnd, (uname != NULL)? uname: winname2uxname(argv[ii]));
         else return loadFile(hnd, (uname != NULL)? uname: winname2uxname(argv[ii]));          else return loadFile(hnd, (uname != NULL)? uname: winname2uxname(argv[ii]));
     }      }
Line 115  int terminateAsir(HWND hnd) {
Line 150  int terminateAsir(HWND hnd) {
 char *winname2uxname(char wname[]) {  char *winname2uxname(char wname[]) {
   int i;    int i;
   char *uname;    char *uname;
     damemsg(wname);
   uname = (char *) malloc(strlen(wname)+1);    uname = (char *) malloc(strlen(wname)+1);
   for (i=0; i<strlen(wname); i++) {    for (i=0; i<strlen(wname); i++) {
     if (wname[i] == '\\') uname[i] = '/';      if (wname[i] == '\\') uname[i] = '/';
Line 146  FILE *findAsirHandler() {
Line 182  FILE *findAsirHandler() {
   char path1[1024];    char path1[1024];
   char path0[1024];    char path0[1024];
   char *path[] = { NULL, NULL, "c:/Program files/asir/bin/asirgui.hnd", "c:/Program files (x86)/asir/bin/asirgui.hnd", "c:/asir/bin/asirgui.hnd" };    char *path[] = { NULL, NULL, "c:/Program files/asir/bin/asirgui.hnd", "c:/Program files (x86)/asir/bin/asirgui.hnd", "c:/asir/bin/asirgui.hnd" };
   sprintf(path0,"%s/asirgui.hnd",(char *)getenv("TEMP"));    sprintf(path0,"%s/asirgui.hnd",getenv("TEMP"));
   path[0]=winname2uxname(path0);    path[0]=winname2uxname(path0);
   if (getenv("ASIR_ROOTDIR") != NULL) {    if (getenv("ASIR_ROOTDIR") != NULL) {
     sprintf(path1,"%s/bin/asirgui.hnd",(char *)getenv("ASIR_ROOTDIR"));      sprintf(path1,"%s/bin/asirgui.hnd",getenv("ASIR_ROOTDIR"));
     path[1]=winname2uxname(path1);      path[1]=winname2uxname(path1);
   }else{    }else{
     sprintf(path1,"%s/Desktop/asir/bin/asirgui.hnd",(char *)getenv("HOMEPATH"));      sprintf(path1,"%s%s/Desktop/asir/bin/asirgui.hnd",getenv("HOMEDRIVE"),getenv("HOMEPATH"));
     path[1]=winname2uxname(path1);      path[1]=winname2uxname(path1);
   }    }
   for(i=0; i<sizeof(path); i++) {    for(i=0; i<sizeof(path); i++) {
Line 201  int pasteFile(HWND hnd, char *uname) {
Line 237  int pasteFile(HWND hnd, char *uname) {
                 return -1;                  return -1;
         }          }
         s = (char *)malloc(size+1);          s = (char *)malloc(size+1);
         i = 0;          i = 0;s[0]=0;
         while ((c=fgetc(fp)) != EOF) {          while ((c=fgetc(fp)) != EOF) {
                 s[i] = c; s[i+1]=0;                  s[i] = c; s[i+1]=0;
                 if (i >= size-1) {                  if (i >= size-1) {
Line 213  int pasteFile(HWND hnd, char *uname) {
Line 249  int pasteFile(HWND hnd, char *uname) {
                 }                  }
                 i++;                  i++;
         }          }
         sendFileName(hnd,s);          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);          free(s);
         PostMessage(hnd,WM_CHAR,0xa,1);          PostMessage(hnd,WM_CHAR,0xa,1);
         return 0;          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);
   }
   
   int damemsg(unsigned char s[]) {
     int dame;
     char msg[1024];
     char s2[3];
     dame = damemoji(s);
     if (dame) {
       s2[0]=s[dame-1]; s2[1]=s[dame]; s2[2]=0;
       sprintf(msg,"The path name %s contains dame-moji %s (ShiftJIS code).",s,s2);
       MessageBox(NULL,TEXT(msg),TEXT("ERROR in cmdasir.c"), MB_OK);
     }
   
   }
   int damemoji(unsigned char s[]) {
     char lang[BUFSIZ];
     int n,i,dame;
     int c;
     lang[0]=0;
     if(GetLocaleInfo(GetUserDefaultLCID(), LOCALE_SISO639LANGNAME, lang, BUFSIZ)){
       // printf("lang=%s\n",lang);
     }
     dame=0;
     if (strcmp(lang,"ja")==0) {
       n = strlen(s);
       for (i=1; i<n; i++) {
         if (s[i] == '\\') {
           c=s[i-1];
           if (((c>=0x81) && (c<=0x9f)) || ((c>=0xe0) && (c<=0xee)) || ((c>=0xfa) && (c<=0xfc))) {
             dame=i; break;
           }
         }
       }
     }
     return(dame);
 }  }

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.11

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