-/*
- * $Source$
- * $Author$
- * $Header$
+/* $Id$
*
- * Copyright (C) 1987 by the Massachusetts Institute of Technology
+ * Perform a Moira query
*
- * $Log$
- * Revision 1.1 1987-06-04 01:29:32 wesommer
- * Initial revision
+ * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology
+ * For copying and distribution information, please see the file
+ * <mit-copyright.h>.
*
*/
-#ifndef lint
-static char *rcsid_sms_query_c = "$Header$";
-#endif lint
+#include <mit-copyright.h>
+#include <moira.h>
+#include "mr_private.h"
+
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+
+RCSID("$Header$");
-#include "sms_private.h"
+/*
+ * This routine is the primary external interface to the mr library.
+ *
+ * It builds a new argument vector with the query handle prepended,
+ * and calls mr_query_internal.
+ */
+static int level = 0;
-int sms_query(name, argc, argv, callproc, callarg)
- char *name; /* Query name */
- int argc; /* Arg count */
- char **argv; /* Args */
- int (*callproc)(); /* Callback procedure */
- char *callarg; /* Callback argument */
+int mr_query(char *name, int argc, char **argv,
+ int (*callproc)(int, char **, void *), void *callarg)
{
- int status;
- sms_params *params = NULL, *reply = NULL;
-
- if (!_sms_conn) {
- return SMS_NOT_CONNECTED;
- }
-
- params = (struct sms_params *) malloc(sizeof(*params));
- params->sms_procno = SMS_QUERY;
- params->sms_argc = 1 + argc;
- params->sms_argl = NULL;
+ int status, stopcallbacks = 0;
+ mr_params params, reply;
+
+ CHECK_CONNECTED;
+ if (level)
+ return MR_QUERY_NOT_REENTRANT;
+
+ params.u.mr_procno = MR_QUERY;
+ params.mr_argc = argc + 1;
+ params.mr_argl = NULL;
+ params.mr_argv = malloc(sizeof(char *) * (argc + 1));
+ if (!params.mr_argv)
+ return ENOMEM;
+ params.mr_argv[0] = name;
+ memcpy(params.mr_argv + 1, argv, sizeof(char *) * argc);
- params->sms_argv = (char **)malloc(sizeof(char *) * params->sms_argc);
+ level++;
+ if ((status = mr_do_call(¶ms, &reply)))
+ goto punt;
- params->sms_argv[0] = name;
- bcopy((char *)argv, (char *)(params->sms_argv + 1),
- sizeof(char *) * argc);
-
- if ((status = sms_do_call(params, &reply)))
- goto punt;
+ while ((status = reply.u.mr_status) == MR_MORE_DATA)
+ {
+ if (callproc && !stopcallbacks)
+ stopcallbacks = (*callproc)(reply.mr_argc, reply.mr_argv, callarg);
+ mr_destroy_reply(reply);
- while ((status = reply->sms_status) == SMS_MORE_DATA) {
- (*callproc)(reply->sms_argc, reply->sms_argv, callarg);
- sms_destroy_reply(reply);
- reply = NULL;
- /*XXX error handling here sucks */
- initialize_operation(_sms_recv_op, sms_start_recv, &reply,
- (int (*)())NULL);
- queue_operation(_sms_conn, CON_INPUT, _sms_recv_op);
- complete_operation(_sms_recv_op);
+ if (mr_receive(_mr_conn, &reply) != MR_SUCCESS)
+ {
+ mr_disconnect();
+ status = MR_ABORTED;
+ goto punt_1;
}
-
+ }
+
punt:
- sms_destroy_reply(reply);
- if(params) {
- if(params->sms_argv)
- free(params->sms_argv);
- if(params->sms_argl)
- free(params->sms_argl);
- free(params);
- }
- return status;
+ mr_destroy_reply(reply);
+punt_1:
+ level--;
+ free(params.mr_argv);
+
+ return status;
}