Annotation of OpenXM_contrib2/windows/post-msg-asirgui/cmdasir.c, Revision 1.11
1.11 ! takayama 1: /* $OpenXM: OpenXM_contrib2/windows/post-msg-asirgui/cmdasir.c,v 1.10 2014/05/25 21:01:04 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>
1.8 ohara 10: #include <fcntl.h>
1.11 ! takayama 11: #include <winnls.h>
1.3 ohara 12:
13: char *winname2uxname(char winname[]);
14: FILE *findAsirHandler();
15: int sendFileName(HWND hnd,char uname[]);
16: int loadFile(HWND hnd, char *uname);
17: int terminateAsir(HWND hnd);
18: int hasTemp(char *s);
19: int pasteFile(HWND hnd, char *uname);
1.6 ohara 20: int containEnd(char s[]);
21: int abortAsir(HWND hnd);
1.7 ohara 22: int main(int argc, char *argv[]);
1.11 ! takayama 23: int damemsg(unsigned char s[]);
! 24: int damemoji(unsigned char s[]);
1.7 ohara 25:
1.8 ohara 26: FILE *open_stdio(DWORD type) {
27: HANDLE hnd;
28: int fd;
29: if((hnd = GetStdHandle(type)) != INVALID_HANDLE_VALUE && (fd = _open_osfhandle((intptr_t)hnd, _O_TEXT)) >= 0) {
30: return _fdopen(fd, "w");
31: }
32: return NULL;
33: }
34:
1.7 ohara 35: int WINAPI WinMain(HINSTANCE hins, HINSTANCE prev, LPSTR arg, int show) {
1.8 ohara 36: FILE *fp;
37: #if (_WIN32_WINNT > 0x0500)
38: if(AttachConsole(ATTACH_PARENT_PROCESS)) {
39: if(fp = open_stdio(STD_OUTPUT_HANDLE)) {
40: *stdout = *fp;
41: setvbuf(stdout, NULL, _IONBF, 0);
42: }
43: }
44: #endif
1.7 ohara 45: main(__argc, __argv);
46: return 0;
47: }
1.3 ohara 48:
49: int main(int argc, char *argv[])
50: {
51: HWND hnd;
52: int c,ii;
53: char *uname=NULL;
54: FILE *fp=NULL;
55: FILE *fp2=NULL;
56: char snameWin[1024];
57: char *snameUx;
58: char msg[1024];
59: char cmd[1024];
60: int paste_contents=0;
1.5 takayama 61: int abort=0;
1.3 ohara 62:
1.10 ohara 63: /* MessageBox(NULL,TEXT("test"),TEXT("ERROR in cmdasir.c"),MB_OK); */
1.8 ohara 64: sprintf(snameWin,"%s\\cmdasir-%d.txt",getenv("TEMP"),_getpid());
1.3 ohara 65: snameUx = winname2uxname(snameWin);
66: if ((argc > 1) && (strcmp(argv[1],"--delete-tmp")==0)) {
67: sprintf(cmd,"del %s\\cmdasir-*",getenv("TEMP"));
68: system(cmd);
69: return(0);
70: }
1.5 takayama 71:
1.3 ohara 72: fp = findAsirHandler();
73: if (fp == NULL) {
1.8 ohara 74: printf("handler file is not found.\n"); return(-1);
1.3 ohara 75: }
76: fscanf(fp,"%d",&hnd);
77: fclose(fp);
78: if ((argc > 1) && (strcmp(argv[1],"--quit")==0)) {
79: return terminateAsir(hnd);
80: }
1.5 takayama 81: if ((argc > 1) && (strcmp(argv[1],"--abort")==0)) {
82: return abortAsir(hnd);
83: }
1.3 ohara 84: printf("Handler is %d\n",hnd);
85: paste_contents=0;
86: for (ii=1; ii<argc; ii++) {
1.5 takayama 87: if (strcmp(argv[ii],"--paste-contents")==0) {
88: paste_contents=1; continue;
89: }
1.3 ohara 90: uname = NULL;
91: if (hasTemp(argv[ii])) {
92: fp = fopen(winname2uxname(argv[ii]),"r");
93: fp2 = fopen(snameUx,"w");
94: if (fp2 == NULL) {
95: sprintf(msg,"File %s to load is not found.",argv[ii]);
1.10 ohara 96: MessageBox(NULL,TEXT(msg),TEXT("ERROR in cmdasir.c"), MB_OK);
1.3 ohara 97: return(-1);
98: }
99: while ((c=fgetc(fp)) >= 0) fputc(c,fp2);
100: if (!paste_contents) fprintf(fp2,"\nend$\n");
101: fclose(fp2);
102: uname=snameUx;
103: }
104: printf("filename=%s\n",argv[ii]);
1.8 ohara 105: if(_access(argv[ii],0)==0) {
1.3 ohara 106: if (paste_contents) return pasteFile(hnd, (uname != NULL)? uname: winname2uxname(argv[ii]));
107: else return loadFile(hnd, (uname != NULL)? uname: winname2uxname(argv[ii]));
108: }
109: }
110: /* work file is kept. Prepare a commnd to delete all the work files under temp
111: */
112: while ((c=getchar()) != EOF) {
113: if (!PostMessage(hnd,WM_CHAR,c,1)) {
114: MessageBox(NULL,TEXT("asirgui is not running."), TEXT("ERROR in cmdasir.c"),MB_OK);
115: return(-1);
116: }
117: }
118: return 0;
119: }
120:
121: int loadFile(HWND hnd, char *uname) {
122: char *s="load(@);\n";
123: int i;
124: for (i=0; i<strlen(s); i++) {
125: if (s[i] != '@') PostMessage(hnd,WM_CHAR,s[i],1);
126: else {
127: PostMessage(hnd,WM_CHAR,'"',1);
128: sendFileName(hnd,uname);
129: if (!PostMessage(hnd,WM_CHAR,'"',1)) {
130: MessageBox(NULL,TEXT("asirgui is not running."),TEXT("ERROR in cmdasir.c"),MB_OK);
131: return -1;
132: }
133: }
134: }
135: return 0;
136: }
137:
138: int terminateAsir(HWND hnd) {
139: int i;
140: char *s="quit;\n";
141: for (i=0; i<strlen(s); i++) {
142: if (!PostMessage(hnd,WM_CHAR,s[i],1)) {
143: MessageBox(NULL,TEXT("asirgui is not running."),TEXT("ERROR in cmdasir.c"),MB_OK);
144: return -1;
145: }
146: }
147: return 0;
148: }
149:
150: char *winname2uxname(char wname[]) {
151: int i;
152: char *uname;
1.11 ! takayama 153: damemsg(wname);
1.3 ohara 154: uname = (char *) malloc(strlen(wname)+1);
155: for (i=0; i<strlen(wname); i++) {
156: if (wname[i] == '\\') uname[i] = '/';
157: else uname[i] = wname[i];
158: uname[i+1] = 0;
159: }
160: return(uname);
161: }
162:
163: #if 0
164: int hasTemp(char *s) {
165: int i;
166: size_t n=strlen(s);
167: for (i=0; i<n-3; i++) {
168: if (strncmp(&(s[i]),"Temp",4)==0) return(1);
169: }
170: return(0);
171: }
172: #endif
173:
174: int hasTemp(char *s) {
175: return strstr(s, "Temp")!=NULL;
176: }
177:
178: FILE *findAsirHandler() {
179: int i;
180: FILE *hnd = NULL;
181: char msg[1024];
182: char path1[1024];
183: char path0[1024];
184: char *path[] = { NULL, NULL, "c:/Program files/asir/bin/asirgui.hnd", "c:/Program files (x86)/asir/bin/asirgui.hnd", "c:/asir/bin/asirgui.hnd" };
1.9 ohara 185: sprintf(path0,"%s/asirgui.hnd",getenv("TEMP"));
1.3 ohara 186: path[0]=winname2uxname(path0);
187: if (getenv("ASIR_ROOTDIR") != NULL) {
1.9 ohara 188: sprintf(path1,"%s/bin/asirgui.hnd",getenv("ASIR_ROOTDIR"));
1.3 ohara 189: path[1]=winname2uxname(path1);
190: }else{
1.9 ohara 191: sprintf(path1,"%s%s/Desktop/asir/bin/asirgui.hnd",getenv("HOMEDRIVE"),getenv("HOMEPATH"));
1.3 ohara 192: path[1]=winname2uxname(path1);
193: }
194: for(i=0; i<sizeof(path); i++) {
195: if(path[i]!=NULL) {
196: hnd = fopen(path[i],"r");
197: if (hnd != NULL) return(hnd);
198: }
199: }
200: sprintf(msg,"asir handler is not found.");
201: MessageBox(NULL,TEXT(msg),TEXT("ERROR in cmdasir.c"),MB_OK);
202: return NULL;
203: }
204: #if 0
205: int sendFileName_ascii(HWND hnd,char uname[]) {
206: int j;
207: for (j=0; j<strlen(uname);j++) PostMessage(hnd,WM_CHAR,uname[j],1);
208: }
209: #endif
210: int sendFileName(HWND hnd,char uname[]) {
211: size_t len=strlen(uname)+1;
212: HGLOBAL hMem;
213: LPTSTR pMem;
214: if (!OpenClipboard(NULL) ) return 1;
215: hMem = GlobalAlloc(GMEM_FIXED,len);
216: pMem = (LPTSTR)hMem;
217: lstrcpy(pMem,(LPCTSTR)uname);
218: EmptyClipboard();
219: SetClipboardData(CF_TEXT,hMem);
220: CloseClipboard();
221: if (!PostMessage(hnd,WM_CHAR,0x19,1)) { /* 0x19 ctrl-Y, ctrl-V 0x16 */
222: MessageBox(NULL,TEXT("asirgui is not running."),TEXT("ERROR in cmdasir.c"),MB_OK);
223: return -1;
224: }
225: return 0;
226: }
227:
228: int pasteFile(HWND hnd, char *uname) {
229: FILE *fp;
230: char *s=NULL;
231: char *sold=NULL;
232: int size=1024;
233: int c,i;
234: fp = fopen(uname,"r");
235: if (fp == NULL) {
236: MessageBox(NULL,TEXT("File is not found: "),TEXT(uname),MB_OK);
237: return -1;
238: }
239: s = (char *)malloc(size+1);
1.4 takayama 240: i = 0;s[0]=0;
1.3 ohara 241: while ((c=fgetc(fp)) != EOF) {
242: s[i] = c; s[i+1]=0;
243: if (i >= size-1) {
244: sold = s;
245: size = size*2;
246: s = (char*)malloc(size+1);
247: strcpy(s,sold);
248: free(sold);
249: }
250: i++;
251: }
1.4 takayama 252: if (containEnd(s)) {
253: MessageBox(NULL,TEXT("The string contains end$ or end;"),TEXT(uname),MB_OK);
254: return(0);
255: }
256: if (strlen(s)>0) sendFileName(hnd,s); else {
257: MessageBox(NULL,TEXT("Empty string: "),TEXT(uname),MB_OK);
258: return 0;
259: }
1.3 ohara 260: free(s);
261: PostMessage(hnd,WM_CHAR,0xa,1);
262: return 0;
263: }
1.4 takayama 264:
1.5 takayama 265: int containEnd(char s[]) {
1.4 takayama 266: int i;
267: for (i=0; i<strlen(s)-4; i++) {
268: if (strncmp(&(s[i]),"end$",4)==0) return 1;
269: if (strncmp(&(s[i]),"end;",4)==0) return 1;
270: }
271: return(0);
272: }
1.5 takayama 273:
1.6 ohara 274: int abortAsir(HWND hnd) {
1.5 takayama 275: int c;
276: int result;
277: result=MessageBox(NULL,TEXT("Do you abort this computation?"),TEXT("Abort"),MB_OKCANCEL);
278: if (result == IDCANCEL) return(0);
279: c=0x3;
280: if (!PostMessage(hnd,WM_CHAR,c,1)) {
281: MessageBox(NULL,TEXT("asirgui is not running."), TEXT("ERROR in cmdasir.c"),MB_OK);
282: return(-1);
283: }
284:
285: printf("Sending ctrl-C\n");
286: /* Sleep(3*1000); */
287: result=MessageBox(NULL,TEXT("Interrupt is displayed in asirgui?"),TEXT("Sent ctrl-C."),MB_OKCANCEL);
288: if (result == IDCANCEL) return(0);
289: c='t'; PostMessage(hnd,WM_CHAR,c,1);
290: c=0xd; PostMessage(hnd,WM_CHAR,c,1);
291: c='y'; PostMessage(hnd,WM_CHAR,c,1);
292: c=0xd; PostMessage(hnd,WM_CHAR,c,1);
293: return(0);
294: }
1.11 ! takayama 295:
! 296: int damemsg(unsigned char s[]) {
! 297: int dame;
! 298: char msg[1024];
! 299: char s2[3];
! 300: dame = damemoji(s);
! 301: if (dame) {
! 302: s2[0]=s[dame-1]; s2[1]=s[dame]; s2[2]=0;
! 303: sprintf(msg,"The path name %s contains dame-moji %s (ShiftJIS code).",s,s2);
! 304: MessageBox(NULL,TEXT(msg),TEXT("ERROR in cmdasir.c"), MB_OK);
! 305: }
! 306:
! 307: }
! 308: int damemoji(unsigned char s[]) {
! 309: char lang[BUFSIZ];
! 310: int n,i,dame;
! 311: int c;
! 312: lang[0]=0;
! 313: if(GetLocaleInfo(GetUserDefaultLCID(), LOCALE_SISO639LANGNAME, lang, BUFSIZ)){
! 314: // printf("lang=%s\n",lang);
! 315: }
! 316: dame=0;
! 317: if (strcmp(lang,"ja")==0) {
! 318: n = strlen(s);
! 319: for (i=1; i<n; i++) {
! 320: if (s[i] == '\\') {
! 321: c=s[i-1];
! 322: if (((c>=0x81) && (c<=0x9f)) || ((c>=0xe0) && (c<=0xee)) || ((c>=0xfa) && (c<=0xfc))) {
! 323: dame=i; break;
! 324: }
! 325: }
! 326: }
! 327: }
! 328: return(dame);
! 329: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>