]> andersk Git - moira.git/blob - server/qsubs.c
second code style cleanup: void/void * usage, proper #includes. try to
[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 Queries2[];
17 extern int QueryCount2;
18
19 int qcmp(const void *q1, const void *q2);
20
21 struct query *get_query_by_name(char *name, int version)
22 {
23   struct query *q;
24   int i;
25
26   q = Queries2;
27   i = QueryCount2;
28
29   if (strlen(name) == 4)
30     {
31       while (--i >= 0)
32         {
33           if (!strcmp(q->shortname, name))
34             return q;
35           q++;
36         }
37     }
38   else
39     {
40       while (--i >= 0)
41         {
42           if (!strcmp(q->name, name))
43             return q;
44           q++;
45         }
46     }
47
48   return NULL;
49 }
50
51 void list_queries(int version, int (*action)(int, char *[], void *),
52                   void *actarg)
53 {
54   struct query *q;
55   int i;
56   static struct query **squeries2 = NULL;
57   struct query **sq;
58   char qnames[80];
59   char *qnp;
60   int count;
61
62   count = QueryCount2;
63   if (!squeries2)
64     {
65       sq = malloc(count * sizeof(struct query *));
66       squeries2 = sq;
67       q = Queries2;
68       for (i = count; --i >= 0; )
69         *sq++ = q++;
70       qsort(squeries2, count, sizeof(struct query *), qcmp);
71     }
72   sq = squeries2;
73
74   qnp = qnames;
75   for (i = count; --i >= 0; sq++)
76     {
77       sprintf(qnames, "%s (%s)", (*sq)->name, (*sq)->shortname);
78       (*action)(1, &qnp, actarg);
79     }
80   strcpy(qnames, "_help");
81   (*action)(1, &qnp, actarg);
82   strcpy(qnames, "_list_queries");
83   (*action)(1, &qnp, actarg);
84   strcpy(qnames, "_list_users");
85   (*action)(1, &qnp, actarg);
86 }
87
88 void help_query(struct query *q, int (*action)(int, char *[], void *),
89                 void *actarg)
90 {
91   int argcount;
92   int i;
93   char argn[32];
94   char qname[512];
95   char argr[512];
96   char *argv[32];
97
98   argcount = q->argc;
99   if (q->type == UPDATE || q->type == APPEND)
100     argcount += q->vcnt;
101
102   switch (argcount)
103     {
104     case 0:
105       sprintf(qname, "   %s, %s ()", q->name, q->shortname);
106       argv[0] = qname;
107       argcount = 1;
108       break;
109
110     case 1:
111       sprintf(qname, "   %s, %s (%s)", q->name, q->shortname, q->fields[0]);
112       argv[0] = qname;
113       argcount = 1;
114       break;
115
116     case 2:
117       sprintf(qname, "   %s, %s (%s, %s)", q->name, q->shortname,
118               q->fields[0], q->fields[1]);
119       argv[0] = qname;
120       argcount = 1;
121       break;
122
123     default:
124       sprintf(qname, "   %s, %s (%s", q->name, q->shortname, q->fields[0]);
125       argv[0] = qname;
126       argcount--;
127       for (i = 1; i < argcount; i++)
128         argv[i] = q->fields[i];
129       sprintf(argn, "%s)", q->fields[argcount]);
130       argv[argcount++] = argn;
131       break;
132     }
133
134   if (q->type == RETRIEVE)
135     {
136       sprintf(argr, "%s => %s", argv[--argcount], q->fields[q->argc]);
137       argv[argcount++] = argr;
138       if (q->vcnt > 1)
139         {
140           for (i = q->argc + 1; i < q->vcnt + q->argc; i++)
141             argv[argcount++] = q->fields[i];
142         }
143     }
144   (*action)(argcount, argv, actarg);
145 }
146
147 int qcmp(const void *q1, const void *q2)
148 {
149   return strcmp((*(struct query **)q1)->name, (*(struct query **)q2)->name);
150 }
This page took 0.05956 seconds and 5 git commands to generate.