]>
Commit | Line | Data |
---|---|---|
7ac48069 | 1 | /* $Id$ |
3e10560e | 2 | * |
7ac48069 | 3 | * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology |
4 | * For copying and distribution information, please see the file | |
5 | * <mit-copyright.h>. | |
3e10560e | 6 | */ |
7 | ||
c801de4c | 8 | #include <mit-copyright.h> |
03c05291 | 9 | #include "mr_server.h" |
97479f6f | 10 | #include "query.h" |
11 | ||
7ac48069 | 12 | #include <stdlib.h> |
13 | ||
14 | RCSID("$Header$"); | |
15 | ||
c44ddfa7 | 16 | extern struct query Queries[]; |
17 | extern int QueryCount; | |
97479f6f | 18 | |
7ac48069 | 19 | int qcmp(const void *q1, const void *q2); |
20 | ||
c44ddfa7 | 21 | struct query *get_query_by_name(char *name, int version) |
97479f6f | 22 | { |
44d12d58 | 23 | int i; |
5eaef520 | 24 | |
c44ddfa7 | 25 | i = QueryCount; |
5eaef520 | 26 | |
27 | if (strlen(name) == 4) | |
28 | { | |
29 | while (--i >= 0) | |
30 | { | |
c44ddfa7 | 31 | if (!strcmp(Queries[i].shortname, name) && |
32 | Queries[i].version <= version) | |
33 | return &Queries[i]; | |
97479f6f | 34 | } |
5eaef520 | 35 | } |
36 | else | |
37 | { | |
38 | while (--i >= 0) | |
39 | { | |
c44ddfa7 | 40 | if (!strcmp(Queries[i].name, name) && |
41 | Queries[i].version <= version) | |
42 | return &Queries[i]; | |
3e10560e | 43 | } |
44 | } | |
97479f6f | 45 | |
5eaef520 | 46 | return NULL; |
97479f6f | 47 | } |
48 | ||
4a1bd4ba | 49 | void list_queries(client *cl, int (*action)(int, char *[], void *), |
50 | void *actarg) | |
ab70c698 | 51 | { |
b1919982 | 52 | struct query **squeries = NULL; |
53 | int qcount; | |
4a1bd4ba | 54 | struct query *q, **sq; |
ab70c698 | 55 | char qnames[80]; |
56 | char *qnp; | |
4a1bd4ba | 57 | int i; |
ab70c698 | 58 | |
b1919982 | 59 | squeries = sq = xmalloc(QueryCount * sizeof(struct query *)); |
60 | q = Queries; | |
61 | for (i = 0; i < QueryCount; i++) | |
5eaef520 | 62 | { |
b1919982 | 63 | if (q->version > cl->version) |
c44ddfa7 | 64 | { |
b1919982 | 65 | q++; |
66 | continue; | |
c44ddfa7 | 67 | } |
b1919982 | 68 | if (i > 0 && strcmp((*sq)->name, q->name)) |
69 | sq++; | |
70 | *sq = q++; | |
5eaef520 | 71 | } |
b1919982 | 72 | qcount = (sq - squeries) + 1; |
73 | qsort(squeries, qcount, sizeof(struct query *), qcmp); | |
74 | ||
4a1bd4ba | 75 | sq = squeries; |
ab70c698 | 76 | |
77 | qnp = qnames; | |
4a1bd4ba | 78 | for (i = qcount; --i >= 0; sq++) |
5eaef520 | 79 | { |
ab70c698 | 80 | sprintf(qnames, "%s (%s)", (*sq)->name, (*sq)->shortname); |
81 | (*action)(1, &qnp, actarg); | |
5eaef520 | 82 | } |
c351960a | 83 | strcpy(qnames, "_help"); |
84 | (*action)(1, &qnp, actarg); | |
85 | strcpy(qnames, "_list_queries"); | |
86 | (*action)(1, &qnp, actarg); | |
87 | strcpy(qnames, "_list_users"); | |
88 | (*action)(1, &qnp, actarg); | |
b1919982 | 89 | |
90 | free(squeries); | |
ab70c698 | 91 | } |
92 | ||
7ac48069 | 93 | void help_query(struct query *q, int (*action)(int, char *[], void *), |
94 | void *actarg) | |
ab70c698 | 95 | { |
44d12d58 | 96 | int argcount; |
97 | int i; | |
5eaef520 | 98 | char argn[32]; |
99 | char qname[512]; | |
100 | char argr[512]; | |
101 | char *argv[32]; | |
ab70c698 | 102 | |
5eaef520 | 103 | argcount = q->argc; |
4ae26bf0 | 104 | if (q->type == MR_Q_UPDATE || q->type == MR_Q_APPEND) |
5eaef520 | 105 | argcount += q->vcnt; |
ab70c698 | 106 | |
5eaef520 | 107 | switch (argcount) |
108 | { | |
ab70c698 | 109 | case 0: |
5eaef520 | 110 | sprintf(qname, " %s, %s ()", q->name, q->shortname); |
111 | argv[0] = qname; | |
112 | argcount = 1; | |
113 | break; | |
ab70c698 | 114 | |
115 | case 1: | |
5eaef520 | 116 | sprintf(qname, " %s, %s (%s)", q->name, q->shortname, q->fields[0]); |
117 | argv[0] = qname; | |
118 | argcount = 1; | |
119 | break; | |
ab70c698 | 120 | |
121 | case 2: | |
5eaef520 | 122 | sprintf(qname, " %s, %s (%s, %s)", q->name, q->shortname, |
123 | q->fields[0], q->fields[1]); | |
124 | argv[0] = qname; | |
125 | argcount = 1; | |
126 | break; | |
ab70c698 | 127 | |
128 | default: | |
5eaef520 | 129 | sprintf(qname, " %s, %s (%s", q->name, q->shortname, q->fields[0]); |
130 | argv[0] = qname; | |
131 | argcount--; | |
132 | for (i = 1; i < argcount; i++) | |
133 | argv[i] = q->fields[i]; | |
134 | sprintf(argn, "%s)", q->fields[argcount]); | |
135 | argv[argcount++] = argn; | |
136 | break; | |
ab70c698 | 137 | } |
5eaef520 | 138 | |
4ae26bf0 | 139 | if (q->type == MR_Q_RETRIEVE) |
5eaef520 | 140 | { |
141 | sprintf(argr, "%s => %s", argv[--argcount], q->fields[q->argc]); | |
142 | argv[argcount++] = argr; | |
143 | if (q->vcnt > 1) | |
144 | { | |
145 | for (i = q->argc + 1; i < q->vcnt + q->argc; i++) | |
146 | argv[argcount++] = q->fields[i]; | |
147 | } | |
c351960a | 148 | } |
5eaef520 | 149 | (*action)(argcount, argv, actarg); |
ab70c698 | 150 | } |
151 | ||
7ac48069 | 152 | int qcmp(const void *q1, const void *q2) |
ab70c698 | 153 | { |
7ac48069 | 154 | return strcmp((*(struct query **)q1)->name, (*(struct query **)q2)->name); |
ab70c698 | 155 | } |