1 | /* $Id: qsubs.c,v 1.20 2008-08-29 16:10:24 zacheiss Exp $ |
---|
2 | * |
---|
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>. |
---|
6 | */ |
---|
7 | |
---|
8 | #include <mit-copyright.h> |
---|
9 | #include "mr_server.h" |
---|
10 | #include "query.h" |
---|
11 | |
---|
12 | #include <stdlib.h> |
---|
13 | |
---|
14 | RCSID("$Header: /afs/athena.mit.edu/astaff/project/moiradev/repository/moira/server/qsubs.c,v 1.20 2008-08-29 16:10:24 zacheiss Exp $"); |
---|
15 | |
---|
16 | extern struct query Queries[]; |
---|
17 | extern int QueryCount; |
---|
18 | |
---|
19 | int qcmp(const void *q1, const void *q2); |
---|
20 | |
---|
21 | struct query *get_query_by_name(char *name, int version) |
---|
22 | { |
---|
23 | int i; |
---|
24 | |
---|
25 | i = QueryCount; |
---|
26 | |
---|
27 | if (strlen(name) == 4) |
---|
28 | { |
---|
29 | while (--i >= 0) |
---|
30 | { |
---|
31 | if (!strcmp(Queries[i].shortname, name) && |
---|
32 | Queries[i].version <= version) |
---|
33 | return &Queries[i]; |
---|
34 | } |
---|
35 | } |
---|
36 | else |
---|
37 | { |
---|
38 | while (--i >= 0) |
---|
39 | { |
---|
40 | if (!strcmp(Queries[i].name, name) && |
---|
41 | Queries[i].version <= version) |
---|
42 | return &Queries[i]; |
---|
43 | } |
---|
44 | } |
---|
45 | |
---|
46 | return NULL; |
---|
47 | } |
---|
48 | |
---|
49 | void list_queries(client *cl, int (*action)(int, char *[], void *), |
---|
50 | void *actarg) |
---|
51 | { |
---|
52 | struct query **squeries = NULL; |
---|
53 | int qcount; |
---|
54 | struct query *q, **sq; |
---|
55 | char qnames[80]; |
---|
56 | char *qnp; |
---|
57 | int i; |
---|
58 | |
---|
59 | squeries = sq = xmalloc(QueryCount * sizeof(struct query *)); |
---|
60 | q = Queries; |
---|
61 | for (i = 0; i < QueryCount; i++) |
---|
62 | { |
---|
63 | if (q->version > cl->version) |
---|
64 | { |
---|
65 | q++; |
---|
66 | continue; |
---|
67 | } |
---|
68 | if (i > 0 && strcmp((*sq)->name, q->name)) |
---|
69 | sq++; |
---|
70 | *sq = q++; |
---|
71 | } |
---|
72 | qcount = (sq - squeries) + 1; |
---|
73 | qsort(squeries, qcount, sizeof(struct query *), qcmp); |
---|
74 | |
---|
75 | sq = squeries; |
---|
76 | |
---|
77 | qnp = qnames; |
---|
78 | for (i = qcount; --i >= 0; sq++) |
---|
79 | { |
---|
80 | sprintf(qnames, "%s (%s)", (*sq)->name, (*sq)->shortname); |
---|
81 | (*action)(1, &qnp, actarg); |
---|
82 | } |
---|
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); |
---|
89 | |
---|
90 | free(squeries); |
---|
91 | } |
---|
92 | |
---|
93 | void help_query(struct query *q, int (*action)(int, char *[], void *), |
---|
94 | void *actarg) |
---|
95 | { |
---|
96 | int argcount; |
---|
97 | int i; |
---|
98 | char argn[32]; |
---|
99 | char qname[512]; |
---|
100 | char argr[512]; |
---|
101 | char *argv[32]; |
---|
102 | |
---|
103 | argcount = q->argc; |
---|
104 | if (q->type == MR_Q_UPDATE || q->type == MR_Q_APPEND) |
---|
105 | argcount += q->vcnt; |
---|
106 | |
---|
107 | switch (argcount) |
---|
108 | { |
---|
109 | case 0: |
---|
110 | sprintf(qname, " %s, %s ()", q->name, q->shortname); |
---|
111 | argv[0] = qname; |
---|
112 | argcount = 1; |
---|
113 | break; |
---|
114 | |
---|
115 | case 1: |
---|
116 | sprintf(qname, " %s, %s (%s)", q->name, q->shortname, q->fields[0]); |
---|
117 | argv[0] = qname; |
---|
118 | argcount = 1; |
---|
119 | break; |
---|
120 | |
---|
121 | case 2: |
---|
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; |
---|
127 | |
---|
128 | default: |
---|
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; |
---|
137 | } |
---|
138 | |
---|
139 | if (q->type == MR_Q_RETRIEVE) |
---|
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 | } |
---|
148 | } |
---|
149 | (*action)(argcount, argv, actarg); |
---|
150 | } |
---|
151 | |
---|
152 | int qcmp(const void *q1, const void *q2) |
---|
153 | { |
---|
154 | return strcmp((*(struct query **)q1)->name, (*(struct query **)q2)->name); |
---|
155 | } |
---|