]>
diplodocus.org Git - nmh/blob - uip/vmhtest.c
3 * vmhtest.c -- test out vmh protocol
11 static struct swit switches
[] = {
24 static int numwins
= 0;
25 static int windows
[NWIN
+ 1];
28 static int selcmds
= 0;
29 #define selcmd() (selcmds++ % 2)
31 static int selwins
= 0;
32 #define selwin() (selwins++ % 2 ? 3 : 1)
36 main (int argc
, char **argv
)
39 char *cp
, buffer
[BUFSIZ
];
40 char **argp
, **arguments
;
43 setlocale(LC_ALL
, "");
45 invo_name
= r1bindex (argv
[0], '/');
47 /* foil search of user profile/context */
48 if (context_foil (NULL
) == -1)
51 arguments
= getarguments (invo_name
, argc
, argv
, 0);
54 while ((cp
= *argp
++))
56 switch (smatch (++cp
, switches
)) {
58 ambigsw (cp
, switches
);
61 adios (NULL
, "-%s unknown", cp
);
64 snprintf (buffer
, sizeof(buffer
), "%s [switches]", invo_name
);
65 print_help (buffer
, switches
, 0);
68 print_version(invo_name
);
72 if (!(cp
= *argp
++) || *cp
== '-')
73 adios (NULL
, "missing argument to %s", argp
[-2]);
74 if ((fd1
= atoi (cp
)) < 1)
75 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
78 if (!(cp
= *argp
++) || *cp
== '-')
79 adios (NULL
, "missing argument to %s", argp
[-2]);
80 if ((fd2
= atoi (cp
)) < 1)
81 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
85 adios (NULL
, "usage: %s [switches]", invo_name
);
104 switch (peer2rc (rc
)) {
107 while (isspace (*bp
))
109 if (sscanf (bp
, "%d", &vrsn
) != 1) {
111 fmt2peer (RC_ERR
, "bad init \"%s\"", rc
->rc_data
);
114 if (vrsn
!= RC_VRSN
) {
115 fmt2peer (RC_ERR
, "version %d unsupported", vrsn
);
119 while (*bp
&& !isspace (*bp
))
121 while (isspace (*bp
))
123 if (sscanf (bp
, "%d", &numwins
) != 1 || numwins
<= 0)
128 for (i
= 1; i
<= numwins
; i
++) {
129 while (*bp
&& !isspace (*bp
))
131 while (isspace (*bp
))
133 if (sscanf (bp
, "%d", &windows
[i
]) != 1 || windows
[i
] <= 0)
136 rc2peer (RC_ACK
, 0, NULL
);
140 adios (NULL
, "%s", rc
->rc_data
);
143 fmt2peer (RC_ERR
, "pINI protocol screw-up");
144 done (1); /* NOTREACHED */
149 static int pLOOP () {
156 switch (peer2rc (rc
)) {
169 adios (NULL
, "%s", rc
->rc_data
);
172 fmt2peer (RC_ERR
, "pLOOP protocol screw-up");
178 static int pQRY (str
)
181 rc2peer (RC_EOF
, 0, NULL
);
186 static int pCMD (str
)
189 if ((selcmd () ? pTTY (str
) : pWIN (str
)) == NOTOK
)
191 rc2peer (RC_EOF
, 0, NULL
);
196 static int pTTY (str
)
204 switch (rc2rc (RC_TTY
, 0, NULL
, rc
)) {
212 adios (NULL
, "%s", rc
->rc_data
);
215 fmt2peer (RC_ERR
, "pTTY protocol screw-up");
221 switch (rc2rc (RC_EOF
, 0, NULL
, rc
)) {
226 adios (NULL
, "%s", rc
->rc_data
);/* NOTREACHED */
229 fmt2peer (RC_ERR
, "pTTY protocol screw-up");
230 done (1); /* NOTREACHED */
235 static int pWIN (str
)
247 snprintf (buffer
, sizeof(buffer
), "%d", selwin ());
248 switch (str2rc (RC_WIN
, buffer
, rc
)) {
256 adios (NULL
, "%s", rc
->rc_data
);
259 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
263 if (pipe (pd
) == NOTOK
) {
264 fmt2peer (RC_ERR
, "no pipes");
268 switch (pid
= vfork ()) {
270 fmt2peer (RC_ERR
, "no forks");
275 open ("/dev/null", O_RDONLY
);
280 execlp ("/bin/sh", "sh", "-c", str
, NULL
);
281 write (2, "no shell\n", strlen ("no shell\n"));
286 while ((i
= read (pd
[0], buffer
, sizeof buffer
)) > 0)
287 switch (rc2rc (RC_DATA
, i
, buffer
, rc
)) {
297 adios (NULL
, "%s", rc
->rc_data
);
300 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
304 switch (rc2rc (RC_EOF
, 0, NULL
, rc
)) {
309 adios (NULL
, "%s", rc
->rc_data
);
312 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
316 fmt2peer (RC_ERR
, "read from pipe lost");
320 return (i
!= NOTOK
? OK
: NOTOK
);