X-Git-Url: http://andersk.mit.edu/gitweb/moira.git/blobdiff_plain/59ec8daed689fed26f4fc6cbd94d5e4d1961c407..767253c8ceabe188a3d43d995cc6763cf2a30700:/lib/mr_query.c diff --git a/lib/mr_query.c b/lib/mr_query.c index d7c5711b..798fbfb8 100644 --- a/lib/mr_query.c +++ b/lib/mr_query.c @@ -1,22 +1,22 @@ -/* - * $Source$ - * $Author$ - * $Header$ +/* $Id$ + * + * Perform a Moira query * - * Copyright (C) 1987 by the Massachusetts Institute of Technology - * For copying and distribution information, please see the file - * . + * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology + * For copying and distribution information, please see the file + * . * */ -#ifndef lint -static char *rcsid_mr_query_c = "$Header$"; -#endif - #include +#include #include "mr_private.h" -#include + +#include #include +#include + +RCSID("$Header$"); /* * This routine is the primary external interface to the mr library. @@ -27,76 +27,47 @@ static char *rcsid_mr_query_c = "$Header$"; static int level = 0; int mr_query(char *name, int argc, char **argv, - int (*callproc)(), char *callarg) + int (*callproc)(int, char **, void *), void *callarg) { - char **nargv = malloc(sizeof(char *) * (argc + 1)); - int status = 0; - - nargv[0] = name; - memcpy(nargv + 1, argv, sizeof(char *) * argc); - status = mr_query_internal(argc + 1, nargv, callproc, callarg); - free(nargv); - return status; -} - -/* - * This routine makes a Moira query. - * - * argv[0] is the query name. - * argv[1..argc-1] are the query arguments. - * - * callproc is called once for each returned value, with arguments - * argc, argv, and callarg. - * If it returns a non-zero value, further calls to it are not done, and - * all future data from the server is ignored (there should be some - * way to send it a quench..) - */ - -int mr_query_internal(int argc, char **argv, int (*callproc)(), char *callarg) -{ - int status; - mr_params params_st; - mr_params *params = NULL; - mr_params *reply = NULL; - int stopcallbacks = 0; + int status, stopcallbacks = 0; + mr_params params, reply; + CHECK_CONNECTED; if (level) return MR_QUERY_NOT_REENTRANT; - CHECK_CONNECTED; - level++; - - params = ¶ms_st; - params->mr_version_no = sending_version_no; - params->mr_procno = MR_QUERY; - params->mr_argc = argc; - params->mr_argl = NULL; - params->mr_argv = argv; + 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); - if ((status = mr_do_call(params, &reply))) + level++; + if ((status = mr_do_call(¶ms, &reply))) goto punt; - while ((status = reply->mr_status) == MR_MORE_DATA) + while ((status = reply.u.mr_status) == MR_MORE_DATA) { - if (!stopcallbacks) - stopcallbacks = (*callproc)(reply->mr_argc, reply->mr_argv, callarg); + if (callproc && !stopcallbacks) + stopcallbacks = (*callproc)(reply.mr_argc, reply.mr_argv, callarg); mr_destroy_reply(reply); - reply = NULL; - initialize_operation(_mr_recv_op, mr_start_recv, &reply, NULL); - queue_operation(_mr_conn, CON_INPUT, _mr_recv_op); - - mr_complete_operation(_mr_recv_op); - if (OP_STATUS(_mr_recv_op) != OP_COMPLETE) + if (mr_receive(_mr_conn, &reply) != MR_SUCCESS) { mr_disconnect(); status = MR_ABORTED; goto punt_1; } } + punt: mr_destroy_reply(reply); punt_1: level--; + free(params.mr_argv); + return status; }