]> andersk Git - moira.git/blobdiff - lib/mr_query.c
Punt restriction against apostrophes in strings: the new server
[moira.git] / lib / mr_query.c
index 6c6b7155e0908f38f799cd33ae69592aa58a4e9c..0058754bc23c9bfe54bf2d29581f5e27c0334e3c 100644 (file)
  *     $Header$
  *
  *     Copyright (C) 1987 by the Massachusetts Institute of Technology
- *
- *     $Log$
- *     Revision 1.1  1987-06-04 01:29:32  wesommer
- *     Initial revision
- *
+ *     For copying and distribution information, please see the file
+ *     <mit-copyright.h>.
+ * 
  */
 
 #ifndef lint
 static char *rcsid_sms_query_c = "$Header$";
 #endif lint
 
-#include "sms_private.h"
+#include <mit-copyright.h>
+#include "mr_private.h"
+#include <string.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.
+ */
+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(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 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;
+    register char **nargv = (char **)malloc(sizeof(char *) * (argc+1));
+    register int status = 0;
+    nargv[0] = name;
+    memcpy((char *)(nargv+1), (char *)argv, sizeof(char *) * argc);
+    status = mr_query_internal(argc+1, nargv, callproc, callarg);
+    free(nargv);
+    return status;
+}
+/*
+ * This routine makes an MR 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(argc, argv, callproc, callarg)
+    int argc;          /* Arg count */
+    char **argv;               /* Args */
+    int (*callproc)(); /* Callback procedure */
+    char *callarg;             /* Callback argument */
+{
+    int status;
+    mr_params params_st;
+    register mr_params *params = NULL;
+    mr_params *reply = NULL;
+    int stopcallbacks = 0;
 
-       params->sms_argv = (char **)malloc(sizeof(char *) * params->sms_argc);
+    if (level) return MR_QUERY_NOT_REENTRANT;
+    
+    CHECK_CONNECTED;
+    level++;
 
-       params->sms_argv[0] = name;
-       bcopy((char *)argv, (char *)(params->sms_argv + 1),
-             sizeof(char *) * argc);
+    params = &params_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;
        
-       if ((status = sms_do_call(params, &reply)))
-               goto punt;
+    if ((status = mr_do_call(params, &reply)))
+       goto punt;
+
+    while ((status = reply->mr_status) == MR_MORE_DATA) {
+       if (!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,
+                            (int (*)())NULL);
+       queue_operation(_mr_conn, CON_INPUT, _mr_recv_op);
 
-       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);
+       mr_complete_operation(_mr_recv_op);
+       if (OP_STATUS(_mr_recv_op) != OP_COMPLETE) {
+           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--;
+    return status;
 }
This page took 0.050654 seconds and 4 git commands to generate.