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