]>
diplodocus.org Git - nmh/blob - uip/vmhtest.c
3 * vmhtest.c -- test out vmh protocol
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
13 static struct swit switches
[] = {
26 static int numwins
= 0;
27 static int windows
[NWIN
+ 1];
30 static int selcmds
= 0;
31 #define selcmd() (selcmds++ % 2)
33 static int selwins
= 0;
34 #define selwin() (selwins++ % 2 ? 3 : 1)
38 main (int argc
, char **argv
)
41 char *cp
, buffer
[BUFSIZ
];
42 char **argp
, **arguments
;
45 setlocale(LC_ALL
, "");
47 invo_name
= r1bindex (argv
[0], '/');
49 /* foil search of user profile/context */
50 if (context_foil (NULL
) == -1)
53 arguments
= getarguments (invo_name
, argc
, argv
, 0);
56 while ((cp
= *argp
++))
58 switch (smatch (++cp
, switches
)) {
60 ambigsw (cp
, switches
);
63 adios (NULL
, "-%s unknown", cp
);
66 snprintf (buffer
, sizeof(buffer
), "%s [switches]", invo_name
);
67 print_help (buffer
, switches
, 0);
70 print_version(invo_name
);
74 if (!(cp
= *argp
++) || *cp
== '-')
75 adios (NULL
, "missing argument to %s", argp
[-2]);
76 if ((fd1
= atoi (cp
)) < 1)
77 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
80 if (!(cp
= *argp
++) || *cp
== '-')
81 adios (NULL
, "missing argument to %s", argp
[-2]);
82 if ((fd2
= atoi (cp
)) < 1)
83 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
87 adios (NULL
, "usage: %s [switches]", invo_name
);
106 switch (peer2rc (rc
)) {
109 while (isspace (*bp
))
111 if (sscanf (bp
, "%d", &vrsn
) != 1) {
113 fmt2peer (RC_ERR
, "bad init \"%s\"", rc
->rc_data
);
116 if (vrsn
!= RC_VRSN
) {
117 fmt2peer (RC_ERR
, "version %d unsupported", vrsn
);
121 while (*bp
&& !isspace (*bp
))
123 while (isspace (*bp
))
125 if (sscanf (bp
, "%d", &numwins
) != 1 || numwins
<= 0)
130 for (i
= 1; i
<= numwins
; i
++) {
131 while (*bp
&& !isspace (*bp
))
133 while (isspace (*bp
))
135 if (sscanf (bp
, "%d", &windows
[i
]) != 1 || windows
[i
] <= 0)
138 rc2peer (RC_ACK
, 0, NULL
);
142 adios (NULL
, "%s", rc
->rc_data
);
145 fmt2peer (RC_ERR
, "pINI protocol screw-up");
146 done (1); /* NOTREACHED */
151 static int pLOOP () {
158 switch (peer2rc (rc
)) {
171 adios (NULL
, "%s", rc
->rc_data
);
174 fmt2peer (RC_ERR
, "pLOOP protocol screw-up");
180 static int pQRY (str
)
183 rc2peer (RC_EOF
, 0, NULL
);
188 static int pCMD (str
)
191 if ((selcmd () ? pTTY (str
) : pWIN (str
)) == NOTOK
)
193 rc2peer (RC_EOF
, 0, NULL
);
198 static int pTTY (str
)
206 switch (rc2rc (RC_TTY
, 0, NULL
, rc
)) {
214 adios (NULL
, "%s", rc
->rc_data
);
217 fmt2peer (RC_ERR
, "pTTY protocol screw-up");
223 switch (rc2rc (RC_EOF
, 0, NULL
, rc
)) {
228 adios (NULL
, "%s", rc
->rc_data
);/* NOTREACHED */
231 fmt2peer (RC_ERR
, "pTTY protocol screw-up");
232 done (1); /* NOTREACHED */
237 static int pWIN (str
)
249 snprintf (buffer
, sizeof(buffer
), "%d", selwin ());
250 switch (str2rc (RC_WIN
, buffer
, rc
)) {
258 adios (NULL
, "%s", rc
->rc_data
);
261 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
265 if (pipe (pd
) == NOTOK
) {
266 fmt2peer (RC_ERR
, "no pipes");
270 switch (pid
= vfork ()) {
272 fmt2peer (RC_ERR
, "no forks");
277 open ("/dev/null", O_RDONLY
);
282 execlp ("/bin/sh", "sh", "-c", str
, NULL
);
283 write (2, "no shell\n", strlen ("no shell\n"));
288 while ((i
= read (pd
[0], buffer
, sizeof buffer
)) > 0)
289 switch (rc2rc (RC_DATA
, i
, buffer
, rc
)) {
299 adios (NULL
, "%s", rc
->rc_data
);
302 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
306 switch (rc2rc (RC_EOF
, 0, NULL
, rc
)) {
311 adios (NULL
, "%s", rc
->rc_data
);
314 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
318 fmt2peer (RC_ERR
, "read from pipe lost");
322 return (i
!= NOTOK
? OK
: NOTOK
);