]> andersk Git - moira.git/blob - server/qsubs.c
Make gtlc version 7; it didn't change any interfaces so we don't need
[moira.git] / server / qsubs.c
1 /* $Id$
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$");
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   static struct query **squeries = NULL;
53   static int qcount;
54   struct query *q, **sq;
55   char qnames[80];
56   char *qnp;
57   int i;
58
59   if (!squeries)
60     {
61       squeries = sq = xmalloc(QueryCount * sizeof(struct query *));
62       q = Queries;
63       for (i = 0; i < QueryCount; i++)
64         {
65           if (q->version > cl->version)
66             continue;
67           if (i > 0 && strcmp((*sq)->name, q->name))
68             sq++;
69           *sq = q++;
70         }
71       qcount = (sq - squeries) + 1;
72       qsort(squeries, qcount, sizeof(struct query *), qcmp);
73     }
74   sq = squeries;
75
76   qnp = qnames;
77   for (i = qcount; --i >= 0; sq++)
78     {
79       sprintf(qnames, "%s (%s)", (*sq)->name, (*sq)->shortname);
80       (*action)(1, &qnp, actarg);
81     }
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);
88 }
89
90 void help_query(struct query *q, int (*action)(int, char *[], void *),
91                 void *actarg)
92 {
93   int argcount;
94   int i;
95   char argn[32];
96   char qname[512];
97   char argr[512];
98   char *argv[32];
99
100   argcount = q->argc;
101   if (q->type == UPDATE || q->type == APPEND)
102     argcount += q->vcnt;
103
104   switch (argcount)
105     {
106     case 0:
107       sprintf(qname, "   %s, %s ()", q->name, q->shortname);
108       argv[0] = qname;
109       argcount = 1;
110       break;
111
112     case 1:
113       sprintf(qname, "   %s, %s (%s)", q->name, q->shortname, q->fields[0]);
114       argv[0] = qname;
115       argcount = 1;
116       break;
117
118     case 2:
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;
124
125     default:
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;
134     }
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         }
145     }
146   (*action)(argcount, argv, actarg);
147 }
148
149 int qcmp(const void *q1, const void *q2)
150 {
151   return strcmp((*(struct query **)q1)->name, (*(struct query **)q2)->name);
152 }
This page took 0.076213 seconds and 5 git commands to generate.