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