|
|
1.1 root 1: #include <sys/types.h>
2: #include <fio.h>
3: #include <ipc.h>
4:
5: #define ustring "usage: scriptcon dest script-file\n"
6:
7: usage()
8: {
9: write(2, ustring, sizeof(ustring)-1);
10: exit(1);
11: }
12:
13: main(ac, av)
14: int ac;
15: char *av[];
16: {
17: int sfd;
18: int cfd;
19: char *lookfor;
20: char *sendback;
21: fd_set fds;
22:
23: if (ac<3)
24: usage();
25: sfd = open(av[2], 0);
26: if (sfd<0){
27: fprint(2, "can't open %s\n", av[2]);
28: exit(1);
29: }
30: cfd = ipcopen(ipcpath(av[1], "dk", ""), "");
31: if (cfd<0){
32: fprintf(stderr, "can't open %s\n", av[2]);
33: exit(1);
34: }
35:
36: /*
37: * do the script
38: */
39: while((lookfor=Frdline(sfd))!=NULL){
40: await(cfd, lookfor);
41: if((sendback=Frdline(sfd))!=NULL)
42: fprint(cfd, "%s\n", sendback);
43: }
44: close(sfd);
45:
46: /*
47: * shuttle bytes back and forth
48: */
49: FD_ZERO(fds);
50: for(;;) {
51: FD_SET(cfd, fds);
52: FD_SET(0, fds);
53: switch(select(NOFILE, &fds, (struct fd_set *)0, 1000)) {
54: case -1:
55: return -1;
56: case 0:
57: continue;
58: }
59: if (FD_ISSET(cfd, fds))
60: if (pass(cfd, 1)<0)
61: exit(0);
62: if (FD_ISSET(0, fds))
63: if (pass(0, cfd)<0)
64: exit(0);
65: }
66: }
67:
68: /*
69: * wait for the specified string on the fd
70: */
71: await(fd, str)
72: int fd;
73: char *str;
74: {
75: char buf[128];
76: int c;
77: int len;
78:
79: if(len>sizeof(buf)-2)
80: len = sizeof(buf)-2;
81: len = strlen(str);
82: for(c=0; c<len; c++)
83: buf[c] = '\0';
84: while((c=Fgetc(fd))>=0){
85: buf[len] = c;
86: strcpy(buf, buf+1);
87: if(strcmp(buf, str)==0)
88: break;
89: }
90: }
91:
92: /*
93: * copy one record from one fd to another
94: */
95: pass(from, to)
96: int from, to;
97: {
98: char buf[4096];
99: int n;
100:
101: if ((n=read(from, buf, sizeof(buf)))<=0)
102: return -1;
103: if (write(to, buf, n)!=n)
104: return -1;
105: return 0;
106: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.