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