+/*
+ * $Source$
+ * $Author$
+ * $Header$
+ *
+ * Copyright (C) 1987 by the Massachusetts Institute of Technology
+ * For copying and distribution information, please see the file
+ * <mit-copyright.h>.
+ *
+ */
+
+#ifndef lint
+static char *rcsid_qsubs_c = "$Header$";
+#endif lint
+
+#include <mit-copyright.h>
+#include <moira.h>
#include "query.h"
-extern struct query Queries[];
-extern int QueryCount;
-extern struct s_query S_Queries[];
-extern int S_QueryCount;
+#ifdef MULTIPROTOCOLS
+extern struct query Queries1[], Queries2[];
+extern int QueryCount1, QueryCount2;
+#else
+extern struct query Queries2[];
+extern int QueryCount2;
+#endif MULTIPROTOCOLS
struct query *
-get_query_by_name(name)
- register char *name;
+get_query_by_name(name, version)
+ register char *name;
+ int version;
{
- register struct query *q;
- register int i;
-
- q = Queries;
- i = QueryCount;
-
- if (strlen(name) == 4) {
- while (--i >= 0) {
- if (!strcmp(q->shortname, name)) return(q);
- q++;
- }
- } else {
- while (--i >= 0) {
- if (!strcmp(q->name, name)) return(q);
- q++;
- }
+ register struct query *q;
+ register int i;
+
+#ifdef MULTIPROTOCOLS
+ if (version == MR_VERSION_1) {
+ q = Queries1;
+ i = QueryCount1;
+ } else {
+#endif
+ q = Queries2;
+ i = QueryCount2;
+#ifdef MULTIPROTOCOLS
+ }
+#endif
+
+ if (strlen(name) == 4) {
+ while (--i >= 0) {
+ if (!strcmp(q->shortname, name)) return(q);
+ q++;
}
-
- return((struct query *)0);
-}
-
-get_input_fields(q, argc, argv)
- register struct query *q;
- int *argc;
- char ***argv;
-{
- *argv = q->fields;
- *argc = q->sargc + q->argc;
- if (q->type == UPDATE || q->type == APPEND)
- *argc += q->vcnt;
-}
-
-get_output_fields(q, argc, argv)
- register struct query *q;
- int *argc;
- char ***argv;
-{
- if (q->type == RETRIEVE) {
- *argc = q->vcnt;
- *argv = &q->fields[q->sargc + q->argc];
- } else {
- *argc = 0;
- *argv = (char **)0;
- }
-}
-
-char *
-get_field(q, argv, name)
- register struct query *q;
- char *argv[];
- char *name;
-{
- register char **fp;
- register char *field;
- register int i;
-
- if (q->type != RETRIEVE) return((char *)0);
-
- if (*name == '*') name++;
- fp = &q->fields[q->sargc + q->argc];
- for (i = 0; i < q->vcnt; i++) {
- field = *fp++;
- if (*field == '*') field++;
- if (!strcmp(field, name)) return(argv[i]);
- }
- return((char *)0);
-}
-
-put_field(q, argv, name, value)
- register struct query *q;
- char *argv[];
- char *name;
- char *value;
-{
- register char **fp;
- register char *field;
- register int i;
- register int n;
-
- n = q->sargc + q->argc;
- if (q->type == UPDATE || q->type == APPEND) n += q->vcnt;
-
- if (*name == '*') name++;
- fp = q->fields;
- for (i = 0; i < n; i++) {
- field = *fp++;
- if (*field == '*') field++;
- if (!strcmp(field, name)) {
- strcpy(argv[i], value);
- return(0);
- }
+ } else {
+ while (--i >= 0) {
+ if (!strcmp(q->name, name)) return(q);
+ q++;
}
- return(-1);
-}
-
-
-/* Generic Queue Routines */
+ }
-struct save_queue *
-sq_create()
-{
- register struct save_queue *sq;
-
- sq = (struct save_queue *)malloc(sizeof (struct save_queue));
- sq->q_next = sq;
- sq->q_prev = sq;
- sq->q_lastget = 0;
- return(sq);
+ return((struct query *)0);
}
-sq_save_data(sq, data)
- register struct save_queue *sq;
- char *data;
+list_queries(version, action, actarg)
+ int version;
+ int (*action)();
+ int actarg;
{
- register struct save_queue *q;
-
- q = (struct save_queue *)malloc(sizeof (struct save_queue));
- q->q_next = sq;
- q->q_prev = sq->q_prev;
- sq->q_prev->q_next = q;
- sq->q_prev = q;
- q->q_data = data;
+ register struct query *q;
+ register int i;
+#ifdef MULTIPROTOCOLS
+ static struct query **squeries1 = (struct query **)0;
+#endif
+ static struct query **squeries2 = (struct query **)0;
+ register struct query **sq;
+ char qnames[80];
+ char *qnp;
+ int count;
+ int qcmp();
+
+#ifdef MULTIPROTOCOLS
+ if (version == MR_VERSION_1) {
+ count = QueryCount1;
+ if (squeries1 == (struct query **)0) {
+ sq = (struct query **)malloc(count * sizeof (struct query *));
+ squeries1 = sq;
+ q = Queries1;
+ for (i = count; --i >= 0; )
+ *sq++ = q++;
+ qsort(squeries1, count, sizeof (struct query *), qcmp);
+ }
+ sq = squeries1;
+ } else {
+#endif
+ count = QueryCount2;
+ if (squeries2 == (struct query **)0) {
+ sq = (struct query **)malloc(count * sizeof (struct query *));
+ squeries2 = sq;
+ q = Queries2;
+ for (i = count; --i >= 0; )
+ *sq++ = q++;
+ qsort(squeries2, count, sizeof (struct query *), qcmp);
+ }
+ sq = squeries2;
+#ifdef MULTIPROTOCOLS
+ }
+#endif
+
+ qnp = qnames;
+ for (i = count; --i >= 0; sq++) {
+ sprintf(qnames, "%s (%s)", (*sq)->name, (*sq)->shortname);
+ (*action)(1, &qnp, actarg);
+ }
+ strcpy(qnames, "_help");
+ (*action)(1, &qnp, actarg);
+ strcpy(qnames, "_list_queries");
+ (*action)(1, &qnp, actarg);
+ strcpy(qnames, "_list_users");
+ (*action)(1, &qnp, actarg);
}
-sq_get_data(sq, data)
- register struct save_queue *sq;
- register char **data;
+help_query(q, action, actarg)
+ register struct query *q;
+ int (*action)();
+ int actarg;
{
- if (sq->q_lastget == (struct save_queue *)0) {
- sq->q_lastget = sq->q_next;
- } else {
- sq->q_lastget = sq->q_lastget->q_next;
- }
-
- if (sq->q_lastget == sq) return(0);
- *data = sq->q_lastget->q_data;
- return(1);
+ register int argcount;
+ register int i;
+ char argn[32];
+ char qname[512];
+ char argr[512];
+ char *argv[32];
+
+ argcount = q->argc;
+ if (q->type == UPDATE || q->type == APPEND) argcount += q->vcnt;
+
+ switch (argcount) {
+ case 0:
+ sprintf(qname, " %s, %s ()", q->name, q->shortname);
+ argv[0] = qname;
+ argcount = 1;
+ break;
+
+ case 1:
+ sprintf(qname, " %s, %s (%s)", q->name, q->shortname, q->fields[0]);
+ argv[0] = qname;
+ argcount = 1;
+ break;
+
+ case 2:
+ sprintf(qname, " %s, %s (%s, %s)", q->name, q->shortname,
+ q->fields[0], q->fields[1]);
+ argv[0] = qname;
+ argcount = 1;
+ break;
+
+ default:
+ sprintf(qname, " %s, %s (%s", q->name, q->shortname, q->fields[0]);
+ argv[0] = qname;
+ argcount--;
+ for (i = 1; i < argcount; i++) argv[i] = q->fields[i];
+ sprintf(argn, "%s)", q->fields[argcount]);
+ argv[argcount++] = argn;
+ break;
+ }
+ if (q->type == RETRIEVE) {
+ sprintf(argr, "%s => %s", argv[--argcount], q->fields[q->argc]);
+ argv[argcount++] = argr;
+ if (q->vcnt > 1)
+ for (i = q->argc + 1; i < q->vcnt + q->argc; i++)
+ argv[argcount++] = q->fields[i];
+ }
+ (*action)(argcount, argv, actarg);
}
-sq_destroy(sq)
- register struct save_queue *sq;
+qcmp(q1, q2)
+ struct query **q1;
+ struct query **q2;
{
- register struct save_queue *q;
-
- for (q = sq->q_next; q != sq; q = sq->q_next) {
- sq->q_next = q->q_next;
- free(q);
- }
- free(sq);
+ return(strcmp((*q1)->name, (*q2)->name));
}