Annotation of OpenXM_contrib2/asir2000/builtin/batch.c.fork, Revision 1.1.1.1
1.1 noro 1: #if !defined(VISUAL)
2:
3: #include <stdio.h>
4: #include <stdlib.h>
5:
6: int exec_file(char *bindir,char *fname) {
7: char command[BUFSIZ];
8: int pid,status;
9:
10: sprintf(command,"%s/asir",bindir);
11: pid = fork();
12: if ( pid ) {
13: wait(&status);
14: } else {
15: setpgid(0,getpid());
16: execlp(command,"asir","-f",fname,0);
17: }
18: }
19:
20: #else
21:
22: #include <windows.h>
23: #include <stdlib.h>
24: #include <stdio.h>
25: #include <io.h>
26: #include <fcntl.h>
27: #include <process.h>
28:
29: static void read_until_eof();
30:
31: static HANDLE hRead,hWrite,hNotify,hNotify_Ack,hIntr,hIntr_Ack,hKill,hProc;
32:
33: int exec_file(char *bindir,char *fname) {
34: int i;
35: char *av[BUFSIZ];
36: char AsirExe[BUFSIZ];
37: DWORD tid,mypid,pid,len;
38: HANDLE hR0,hW0,hR1,hW1;
39: SECURITY_ATTRIBUTES SecurityAttributes;
40: char remread[10],remwrite[10];
41: char notify[100],notify_ack[100],intr[100],intr_ack[100],kill[100];
42:
43: mypid = GetCurrentProcessId();
44: sprintf(notify,"asir_notify_%d",mypid);
45: sprintf(notify_ack,"asir_ack_%d",mypid);
46: sprintf(intr,"asir_intr_%d",mypid);
47: sprintf(intr_ack,"asir_intr_ack_%d",mypid);
48: sprintf(kill,"asir_kill_%d",mypid);
49: hNotify = CreateEvent(NULL,TRUE,FALSE,notify);
50: hNotify_Ack = CreateEvent(NULL,TRUE,FALSE,notify_ack);
51: hIntr = CreateEvent(NULL,TRUE,FALSE,intr);
52: hIntr_Ack = CreateEvent(NULL,TRUE,FALSE,intr_ack);
53: hKill = CreateEvent(NULL,TRUE,FALSE,kill);
54:
55: SecurityAttributes.nLength = sizeof(SecurityAttributes);
56: SecurityAttributes.lpSecurityDescriptor = NULL;
57: SecurityAttributes.bInheritHandle = TRUE;
58: CreatePipe(&hR0, &hW0, &SecurityAttributes, 65536);
59: CreatePipe(&hR1, &hW1, &SecurityAttributes, 65536);
60:
61: hRead = hR0;
62: hWrite = hW1;
63: sprintf(remread,"%d",(DWORD)hR1);
64: sprintf(remwrite,"%d",(DWORD)hW0);
65:
66: sprintf(AsirExe,"%s\\engine32.exe",bindir);
67: _setargv();
68: av[0] = "asir";
69: av[1] = remread;
70: av[2] = remwrite;
71: av[3] = notify;
72: av[4] = notify_ack;
73: av[5] = intr;
74: av[6] = intr_ack;
75: av[7] = kill;
76: av[8] = "-norc";
77: av[9] = "-f";
78: av[10] = fname;
79: av[11] = NULL;
80: hProc = (HANDLE)_spawnv(_P_NOWAIT,AsirExe,av);
81: if ( hProc == (HANDLE)-1 ) {
82: fprintf(stderr,"%s がありません",AsirExe);
83: return 0;
84: }
85: read_until_eof();
86: return 1;
87: }
88:
89: int doing_batch = 0;
90: static char insert_buf[BUFSIZ*10];
91: static int insert_buf_len = 0;
92:
93: static void read_until_eof()
94: {
95: char *p;
96: DWORD len;
97: unsigned int cmdsize,cmd,size;
98:
99: doing_batch = 1;
100: while ( 1 ) {
101: WaitForSingleObject(hNotify,(DWORD)-1);
102: ResetEvent(hNotify);
103: SetEvent(hNotify_Ack);
104: ReadFile(hRead,&cmdsize,sizeof(unsigned int),&len,NULL);
105: cmd = cmdsize>>16;
106: size = cmdsize&0xffff;
107: if ( !cmd ) {
108: doing_batch = 0;
109: return;
110: } else if ( size )
111: ReadFile(hRead,insert_buf,size,&len,NULL);
112: }
113: }
114:
115: void send_intr() {
116: PulseEvent(hIntr);
117: }
118:
119: void terminate_batch() {
120: TerminateProcess(hProc,0);
121: }
122: #endif /* VISUAL */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>