]> andersk Git - moira.git/blame - lib/mr_query.c
Change `SMS' to `Moira' where possible.
[moira.git] / lib / mr_query.c
CommitLineData
92eb0760 1/*
2 * $Source$
3 * $Author$
4 * $Header$
5 *
6 * Copyright (C) 1987 by the Massachusetts Institute of Technology
babbc197 7 * For copying and distribution information, please see the file
8 * <mit-copyright.h>.
5eaef520 9 *
92eb0760 10 */
11
12#ifndef lint
59ec8dae 13static char *rcsid_mr_query_c = "$Header$";
a43ce477 14#endif
92eb0760 15
babbc197 16#include <mit-copyright.h>
8defc06b 17#include "mr_private.h"
8fd777cf 18#include <string.h>
a43ce477 19#include <stdlib.h>
92eb0760 20
83e80378 21/*
8defc06b 22 * This routine is the primary external interface to the mr library.
83e80378 23 *
24 * It builds a new argument vector with the query handle prepended,
8defc06b 25 * and calls mr_query_internal.
83e80378 26 */
a43ce477 27static int level = 0;
83e80378 28
5eaef520 29int mr_query(char *name, int argc, char **argv,
30 int (*callproc)(), char *callarg)
92eb0760 31{
44d12d58 32 char **nargv = malloc(sizeof(char *) * (argc + 1));
33 int status = 0;
5eaef520 34
35 nargv[0] = name;
36 memcpy(nargv + 1, argv, sizeof(char *) * argc);
37 status = mr_query_internal(argc + 1, nargv, callproc, callarg);
38 free(nargv);
39 return status;
83e80378 40}
5eaef520 41
83e80378 42/*
59ec8dae 43 * This routine makes a Moira query.
83e80378 44 *
45 * argv[0] is the query name.
46 * argv[1..argc-1] are the query arguments.
47 *
48 * callproc is called once for each returned value, with arguments
49 * argc, argv, and callarg.
50 * If it returns a non-zero value, further calls to it are not done, and
51 * all future data from the server is ignored (there should be some
52 * way to send it a quench..)
53 */
92eb0760 54
5eaef520 55int mr_query_internal(int argc, char **argv, int (*callproc)(), char *callarg)
83e80378 56{
5eaef520 57 int status;
58 mr_params params_st;
44d12d58 59 mr_params *params = NULL;
5eaef520 60 mr_params *reply = NULL;
61 int stopcallbacks = 0;
62
63 if (level)
64 return MR_QUERY_NOT_REENTRANT;
65
66 CHECK_CONNECTED;
67 level++;
4bff4d9d 68
5eaef520 69 params = &params_st;
70 params->mr_version_no = sending_version_no;
71 params->mr_procno = MR_QUERY;
72 params->mr_argc = argc;
73 params->mr_argl = NULL;
74 params->mr_argv = argv;
92eb0760 75
5eaef520 76 if ((status = mr_do_call(params, &reply)))
77 goto punt;
92eb0760 78
5eaef520 79 while ((status = reply->mr_status) == MR_MORE_DATA)
80 {
81 if (!stopcallbacks)
82 stopcallbacks = (*callproc)(reply->mr_argc, reply->mr_argv, callarg);
83 mr_destroy_reply(reply);
84 reply = NULL;
83e80378 85
5eaef520 86 initialize_operation(_mr_recv_op, mr_start_recv, &reply, NULL);
87 queue_operation(_mr_conn, CON_INPUT, _mr_recv_op);
83e80378 88
5eaef520 89 mr_complete_operation(_mr_recv_op);
90 if (OP_STATUS(_mr_recv_op) != OP_COMPLETE)
91 {
92 mr_disconnect();
93 status = MR_ABORTED;
94 goto punt_1;
92eb0760 95 }
5eaef520 96 }
83e80378 97punt:
5eaef520 98 mr_destroy_reply(reply);
4bff4d9d 99punt_1:
5eaef520 100 level--;
101 return status;
92eb0760 102}
This page took 0.229283 seconds and 5 git commands to generate.