1 /* vmhtest.c - test out vmh protocol */
3 static char ident[] = "@(#)$Id: vmhtest.c,v 1.2 1992/11/04 01:06:43 jromine Exp $";
7 #include "../h/vmhsbr.h"
16 static struct swit switches[] = {
31 static int numwins = 0;
32 static int windows[NWIN + 1];
35 static int selcmds = 0;
36 #define selcmd() (selcmds++ % 2)
38 static int selwins = 0;
39 #define selwin() (selwins++ % 2 ? 3 : 1)
43 main (argc, argv, envp)
57 setlocale(LC_ALL, "");
59 invo_name = r1bindex (argv[0], '/');
66 switch (smatch (++cp, switches)) {
68 ambigsw (cp, switches);
71 adios (NULLCP, "-%s unknown", cp);
73 (void) sprintf (buffer, "%s [switches]", invo_name);
74 help (buffer, switches);
78 if (!(cp = *argp++) || *cp == '-')
79 adios (NULLCP, "missing argument to %s", argp[-2]);
80 if ((fd1 = atoi (cp)) < 1)
81 adios (NULLCP, "bad argument %s %s", argp[-2], cp);
84 if (!(cp = *argp++) || *cp == '-')
85 adios (NULLCP, "missing argument to %s", argp[-2]);
86 if ((fd2 = atoi (cp)) < 1)
87 adios (NULLCP, "bad argument %s %s", argp[-2], cp);
91 adios (NULLCP, "usage: %s [switches]", invo_name);
95 (void) rcinit (fd1, fd2);
113 switch (peer2rc (rc)) {
116 while (isspace (*bp))
118 if (sscanf (bp, "%d", &vrsn) != 1) {
120 (void) fmt2peer (RC_ERR, "bad init \"%s\"", rc -> rc_data);
123 if (vrsn != RC_VRSN) {
124 (void) fmt2peer (RC_ERR, "version %d unsupported", vrsn);
128 while (*bp && !isspace (*bp))
130 while (isspace (*bp))
132 if (sscanf (bp, "%d", &numwins) != 1 || numwins <= 0)
137 for (i = 1; i <= numwins; i++) {
138 while (*bp && !isspace (*bp))
140 while (isspace (*bp))
142 if (sscanf (bp, "%d", &windows[i]) != 1 || windows[i] <= 0)
145 (void) rc2peer (RC_ACK, 0, NULLCP);
149 adios (NULLCP, "%s", rc -> rc_data);
152 (void) fmt2peer (RC_ERR, "pINI protocol screw-up");
153 done (1); /* NOTREACHED */
159 static int pLOOP () {
166 switch (peer2rc (rc)) {
168 (void) pQRY (rc -> rc_data);
172 (void) pCMD (rc -> rc_data);
179 adios (NULLCP, "%s", rc -> rc_data);
182 (void) fmt2peer (RC_ERR, "pLOOP protocol screw-up");
189 static int pQRY (str)
192 (void) rc2peer (RC_EOF, 0, NULLCP);
198 static int pCMD (str)
201 if ((selcmd () ? pTTY (str) : pWIN (str)) == NOTOK)
203 (void) rc2peer (RC_EOF, 0, NULLCP);
209 static int pTTY (str)
217 switch (rc2rc (RC_TTY, 0, NULLCP, rc)) {
225 adios (NULLCP, "%s", rc -> rc_data);
228 (void) fmt2peer (RC_ERR, "pTTY protocol screw-up");
234 switch (rc2rc (RC_EOF, 0, NULLCP, rc)) {
239 adios (NULLCP, "%s", rc -> rc_data);/* NOTREACHED */
242 (void) fmt2peer (RC_ERR, "pTTY protocol screw-up");
243 done (1); /* NOTREACHED */
249 static int pWIN (str)
261 (void) sprintf (buffer, "%d", selwin ());
262 switch (str2rc (RC_WIN, buffer, rc)) {
270 adios (NULLCP, "%s", rc -> rc_data);
273 (void) fmt2peer (RC_ERR, "pWIN protocol screw-up");
277 if (pipe (pd) == NOTOK) {
278 (void) fmt2peer (RC_ERR, "no pipes");
282 switch (pid = vfork ()) {
284 (void) fmt2peer (RC_ERR, "no forks");
289 (void) open ("/dev/null", 0);
290 (void) dup2 (pd[1], 1);
291 (void) dup2 (pd[1], 2);
292 (void) close (pd[0]);
293 (void) close (pd[1]);
294 execlp ("/bin/sh", "sh", "-c", str, NULLCP);
295 write (2, "no shell\n", strlen ("no shell\n"));
299 (void) close (pd[1]);
300 while ((i = read (pd[0], buffer, sizeof buffer)) > 0)
301 switch (rc2rc (RC_DATA, i, buffer, rc)) {
306 (void) close (pd[0]);
307 (void) pidwait (pid, OK);
311 adios (NULLCP, "%s", rc -> rc_data);
314 (void) fmt2peer (RC_ERR, "pWIN protocol screw-up");
318 switch (rc2rc (RC_EOF, 0, NULLCP, rc)) {
323 adios (NULLCP, "%s", rc -> rc_data);
326 (void) fmt2peer (RC_ERR, "pWIN protocol screw-up");
330 (void) fmt2peer (RC_ERR, "read from pipe lost");
332 (void) close (pd[0]);
333 (void) pidwait (pid, OK);
334 return (i != NOTOK ? OK : NOTOK);