+static int
+remote_query (query_type, buf, outbuf, bufsiz)
+ char query_type;
+ char *buf;
+ char *outbuf;
+ int *bufsiz;
+{
+ int i;
+ char buf2[PBUFSIZ];
+ char *p2 = &buf2[0];
+ char *p = buf;
+
+ if (! bufsiz)
+ error ("null pointer to remote bufer size specified");
+
+ /* minimum outbuf size is PBUFSIZE - if bufsiz is not large enough let
+ the caller know and return what the minimum size is */
+ /* Note: a zero bufsiz can be used to query the minimum buffer size */
+ if ( *bufsiz < PBUFSIZ )
+ {
+ *bufsiz = PBUFSIZ;
+ return -1;
+ }
+
+ /* except for querying the minimum buffer size, target must be open */
+ if (! remote_desc)
+ error ("remote query is only available after target open");
+
+ /* we only take uppercase letters as query types, at least for now */
+ if ( (query_type < 'A') || (query_type > 'Z') )
+ error ("invalid remote query type");
+
+ if (! buf)
+ error ("null remote query specified");
+
+ if (! outbuf)
+ error ("remote query requires a buffer to receive data");
+
+ outbuf[0] = '\0';
+
+ *p2++ = 'q';
+ *p2++ = query_type;
+
+ /* we used one buffer char for the remote protocol q command and another
+ for the query type. As the remote protocol encapsulation uses 4 chars
+ we have PBUFZIZ -6 left to pack the query string */
+ i = 0;
+ while ( buf[i] && (i < (PBUFSIZ - 7)) )
+ {
+ /* bad caller may have sent forbidden characters */
+ if ( (!isprint(buf[i])) || (buf[i] == '$') || (buf[i] == '#') )
+ error ("illegal characters in query string");
+
+ *p2++ = buf[i];
+ i++;
+ }
+ *p2 = buf[i];
+
+ if ( buf[i] )
+ error ("query larger than avaiable buffer");
+
+ i = putpkt (buf2);
+ if ( i < 0 ) return i;
+
+ getpkt (outbuf, 0);
+
+ return 0;
+}
+