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