3 * vmhtest.c -- test out vmh protocol
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
15 static struct swit switches[] = {
28 static int numwins = 0;
29 static int windows[NWIN + 1];
32 static int selcmds = 0;
33 #define selcmd() (selcmds++ % 2)
35 static int selwins = 0;
36 #define selwin() (selwins++ % 2 ? 3 : 1)
40 main (int argc, char **argv)
43 char *cp, buffer[BUFSIZ];
44 char **argp, **arguments;
47 setlocale(LC_ALL, "");
49 invo_name = r1bindex (argv[0], '/');
51 /* foil search of user profile/context */
52 if (context_foil (NULL) == -1)
55 arguments = getarguments (invo_name, argc, argv, 0);
58 while ((cp = *argp++))
60 switch (smatch (++cp, switches)) {
62 ambigsw (cp, switches);
65 adios (NULL, "-%s unknown", cp);
68 snprintf (buffer, sizeof(buffer), "%s [switches]", invo_name);
69 print_help (buffer, switches, 0);
72 print_version(invo_name);
76 if (!(cp = *argp++) || *cp == '-')
77 adios (NULL, "missing argument to %s", argp[-2]);
78 if ((fd1 = atoi (cp)) < 1)
79 adios (NULL, "bad argument %s %s", argp[-2], cp);
82 if (!(cp = *argp++) || *cp == '-')
83 adios (NULL, "missing argument to %s", argp[-2]);
84 if ((fd2 = atoi (cp)) < 1)
85 adios (NULL, "bad argument %s %s", argp[-2], cp);
89 adios (NULL, "usage: %s [switches]", invo_name);
108 switch (peer2rc (rc)) {
111 while (isspace (*bp))
113 if (sscanf (bp, "%d", &vrsn) != 1) {
115 fmt2peer (RC_ERR, "bad init \"%s\"", rc->rc_data);
118 if (vrsn != RC_VRSN) {
119 fmt2peer (RC_ERR, "version %d unsupported", vrsn);
123 while (*bp && !isspace (*bp))
125 while (isspace (*bp))
127 if (sscanf (bp, "%d", &numwins) != 1 || numwins <= 0)
132 for (i = 1; i <= numwins; i++) {
133 while (*bp && !isspace (*bp))
135 while (isspace (*bp))
137 if (sscanf (bp, "%d", &windows[i]) != 1 || windows[i] <= 0)
140 rc2peer (RC_ACK, 0, NULL);
144 adios (NULL, "%s", rc->rc_data);
147 fmt2peer (RC_ERR, "pINI protocol screw-up");
148 done (1); /* NOTREACHED */
153 static int pLOOP () {
160 switch (peer2rc (rc)) {
173 adios (NULL, "%s", rc->rc_data);
176 fmt2peer (RC_ERR, "pLOOP protocol screw-up");
182 static int pQRY (str)
185 rc2peer (RC_EOF, 0, NULL);
190 static int pCMD (str)
193 if ((selcmd () ? pTTY (str) : pWIN (str)) == NOTOK)
195 rc2peer (RC_EOF, 0, NULL);
200 static int pTTY (str)
208 switch (rc2rc (RC_TTY, 0, NULL, rc)) {
216 adios (NULL, "%s", rc->rc_data);
219 fmt2peer (RC_ERR, "pTTY protocol screw-up");
225 switch (rc2rc (RC_EOF, 0, NULL, rc)) {
230 adios (NULL, "%s", rc->rc_data);/* NOTREACHED */
233 fmt2peer (RC_ERR, "pTTY protocol screw-up");
234 done (1); /* NOTREACHED */
239 static int pWIN (str)
251 snprintf (buffer, sizeof(buffer), "%d", selwin ());
252 switch (str2rc (RC_WIN, buffer, rc)) {
260 adios (NULL, "%s", rc->rc_data);
263 fmt2peer (RC_ERR, "pWIN protocol screw-up");
267 if (pipe (pd) == NOTOK) {
268 fmt2peer (RC_ERR, "no pipes");
272 switch (pid = vfork ()) {
274 fmt2peer (RC_ERR, "no forks");
279 open ("/dev/null", O_RDONLY);
284 execlp ("/bin/sh", "sh", "-c", str, NULL);
285 write (2, "no shell\n", strlen ("no shell\n"));
290 while ((i = read (pd[0], buffer, sizeof buffer)) > 0)
291 switch (rc2rc (RC_DATA, i, buffer, rc)) {
301 adios (NULL, "%s", rc->rc_data);
304 fmt2peer (RC_ERR, "pWIN protocol screw-up");
308 switch (rc2rc (RC_EOF, 0, NULL, rc)) {
313 adios (NULL, "%s", rc->rc_data);
316 fmt2peer (RC_ERR, "pWIN protocol screw-up");
320 fmt2peer (RC_ERR, "read from pipe lost");
324 return (i != NOTOK ? OK : NOTOK);