]> andersk Git - moira.git/blob - lib/mr_query.c
3e74d14d50e942c524e39b01eb289b42416c381c
[moira.git] / lib / mr_query.c
1 /*
2  *      $Source$
3  *      $Author$
4  *      $Header$
5  *
6  *      Copyright (C) 1987 by the Massachusetts Institute of Technology
7  *      For copying and distribution information, please see the file
8  *      <mit-copyright.h>.
9  *
10  */
11
12 #ifndef lint
13 static char *rcsid_sms_query_c = "$Header$";
14 #endif
15
16 #include <mit-copyright.h>
17 #include "mr_private.h"
18 #include <string.h>
19 #include <stdlib.h>
20
21 /*
22  * This routine is the primary external interface to the mr library.
23  *
24  * It builds a new argument vector with the query handle prepended,
25  * and calls mr_query_internal.
26  */
27 static int level = 0;
28
29 int mr_query(char *name, int argc, char **argv,
30              int (*callproc)(), char *callarg)
31 {
32   register char **nargv = malloc(sizeof(char *) * (argc + 1));
33   register int status = 0;
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;
40 }
41
42 /*
43  * This routine makes an MR query.
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  */
54
55 int mr_query_internal(int argc, char **argv, int (*callproc)(), char *callarg)
56 {
57   int status;
58   mr_params params_st;
59   register mr_params *params = NULL;
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++;
68
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;
75
76   if ((status = mr_do_call(params, &reply)))
77     goto punt;
78
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;
85
86       initialize_operation(_mr_recv_op, mr_start_recv, &reply, NULL);
87       queue_operation(_mr_conn, CON_INPUT, _mr_recv_op);
88
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;
95         }
96     }
97 punt:
98   mr_destroy_reply(reply);
99 punt_1:
100   level--;
101   return status;
102 }
This page took 0.028856 seconds and 3 git commands to generate.