]>
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 | { |
4a1bd4ba | 52 | static struct query **squeries = NULL; |
53 | static int qcount; | |
54 | struct query *q, **sq; | |
ab70c698 | 55 | char qnames[80]; |
56 | char *qnp; | |
4a1bd4ba | 57 | int i; |
ab70c698 | 58 | |
4a1bd4ba | 59 | if (!squeries) |
5eaef520 | 60 | { |
4a1bd4ba | 61 | squeries = sq = xmalloc(QueryCount * sizeof(struct query *)); |
c44ddfa7 | 62 | q = Queries; |
63 | for (i = 0; i < QueryCount; i++) | |
64 | { | |
4a1bd4ba | 65 | if (q->version > cl->version) |
66 | continue; | |
c44ddfa7 | 67 | if (i > 0 && strcmp((*sq)->name, q->name)) |
4a1bd4ba | 68 | sq++; |
c44ddfa7 | 69 | *sq = q++; |
70 | } | |
4a1bd4ba | 71 | qcount = (sq - squeries) + 1; |
72 | qsort(squeries, qcount, sizeof(struct query *), qcmp); | |
5eaef520 | 73 | } |
4a1bd4ba | 74 | sq = squeries; |
ab70c698 | 75 | |
76 | qnp = qnames; | |
4a1bd4ba | 77 | for (i = qcount; --i >= 0; sq++) |
5eaef520 | 78 | { |
ab70c698 | 79 | sprintf(qnames, "%s (%s)", (*sq)->name, (*sq)->shortname); |
80 | (*action)(1, &qnp, actarg); | |
5eaef520 | 81 | } |
c351960a | 82 | strcpy(qnames, "_help"); |
83 | (*action)(1, &qnp, actarg); | |
84 | strcpy(qnames, "_list_queries"); | |
85 | (*action)(1, &qnp, actarg); | |
86 | strcpy(qnames, "_list_users"); | |
87 | (*action)(1, &qnp, actarg); | |
ab70c698 | 88 | } |
89 | ||
7ac48069 | 90 | void help_query(struct query *q, int (*action)(int, char *[], void *), |
91 | void *actarg) | |
ab70c698 | 92 | { |
44d12d58 | 93 | int argcount; |
94 | int i; | |
5eaef520 | 95 | char argn[32]; |
96 | char qname[512]; | |
97 | char argr[512]; | |
98 | char *argv[32]; | |
ab70c698 | 99 | |
5eaef520 | 100 | argcount = q->argc; |
101 | if (q->type == UPDATE || q->type == APPEND) | |
102 | argcount += q->vcnt; | |
ab70c698 | 103 | |
5eaef520 | 104 | switch (argcount) |
105 | { | |
ab70c698 | 106 | case 0: |
5eaef520 | 107 | sprintf(qname, " %s, %s ()", q->name, q->shortname); |
108 | argv[0] = qname; | |
109 | argcount = 1; | |
110 | break; | |
ab70c698 | 111 | |
112 | case 1: | |
5eaef520 | 113 | sprintf(qname, " %s, %s (%s)", q->name, q->shortname, q->fields[0]); |
114 | argv[0] = qname; | |
115 | argcount = 1; | |
116 | break; | |
ab70c698 | 117 | |
118 | case 2: | |
5eaef520 | 119 | sprintf(qname, " %s, %s (%s, %s)", q->name, q->shortname, |
120 | q->fields[0], q->fields[1]); | |
121 | argv[0] = qname; | |
122 | argcount = 1; | |
123 | break; | |
ab70c698 | 124 | |
125 | default: | |
5eaef520 | 126 | sprintf(qname, " %s, %s (%s", q->name, q->shortname, q->fields[0]); |
127 | argv[0] = qname; | |
128 | argcount--; | |
129 | for (i = 1; i < argcount; i++) | |
130 | argv[i] = q->fields[i]; | |
131 | sprintf(argn, "%s)", q->fields[argcount]); | |
132 | argv[argcount++] = argn; | |
133 | break; | |
ab70c698 | 134 | } |
5eaef520 | 135 | |
136 | if (q->type == RETRIEVE) | |
137 | { | |
138 | sprintf(argr, "%s => %s", argv[--argcount], q->fields[q->argc]); | |
139 | argv[argcount++] = argr; | |
140 | if (q->vcnt > 1) | |
141 | { | |
142 | for (i = q->argc + 1; i < q->vcnt + q->argc; i++) | |
143 | argv[argcount++] = q->fields[i]; | |
144 | } | |
c351960a | 145 | } |
5eaef520 | 146 | (*action)(argcount, argv, actarg); |
ab70c698 | 147 | } |
148 | ||
7ac48069 | 149 | int qcmp(const void *q1, const void *q2) |
ab70c698 | 150 | { |
7ac48069 | 151 | return strcmp((*(struct query **)q1)->name, (*(struct query **)q2)->name); |
ab70c698 | 152 | } |