source: trunk/third/moira/server/qsubs.c @ 24319

Revision 24319, 3.3 KB checked in by broder, 15 years ago (diff)
New Moira snapshot from SVN.
Line 
1/* $Id: qsubs.c 3956 2010-01-05 20:56:56Z zacheiss $
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#include <string.h>
14
15RCSID("$HeadURL: svn+ssh://svn.mit.edu/moira/trunk/moira/server/qsubs.c $ $Id: qsubs.c 3956 2010-01-05 20:56:56Z zacheiss $");
16
17extern struct query Queries[];
18extern int QueryCount;
19
20int qcmp(const void *q1, const void *q2);
21
22struct query *get_query_by_name(char *name, int version)
23{
24  int i;
25
26  i = QueryCount;
27
28  if (strlen(name) == 4)
29    {
30      while (--i >= 0)
31        {
32          if (!strcmp(Queries[i].shortname, name) &&
33              Queries[i].version <= version)
34            return &Queries[i];
35        }
36    }
37  else
38    {
39      while (--i >= 0)
40        {
41          if (!strcmp(Queries[i].name, name) &&
42              Queries[i].version <= version)
43            return &Queries[i];
44        }
45    }
46
47  return NULL;
48}
49
50void list_queries(client *cl, int (*action)(int, char *[], void *),
51                  void *actarg)
52{
53  struct query **squeries = NULL;
54  int qcount;
55  struct query *q, **sq;
56  char qnames[80];
57  char *qnp;
58  int i;
59
60  squeries = sq = xmalloc(QueryCount * sizeof(struct query *));
61  q = Queries;
62  for (i = 0; i < QueryCount; i++)
63    {
64      if (q->version > cl->version)
65        {
66          q++;
67          continue;
68        }
69      if (i > 0 && strcmp((*sq)->name, q->name))
70        sq++;
71      *sq = q++;
72    }
73  qcount = (sq - squeries) + 1;
74  qsort(squeries, qcount, sizeof(struct query *), qcmp);
75
76  sq = squeries;
77
78  qnp = qnames;
79  for (i = qcount; --i >= 0; sq++)
80    {
81      sprintf(qnames, "%s (%s)", (*sq)->name, (*sq)->shortname);
82      (*action)(1, &qnp, actarg);
83    }
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);
90
91  free(squeries);
92}
93
94void help_query(struct query *q, int (*action)(int, char *[], void *),
95                void *actarg)
96{
97  int argcount;
98  int i;
99  char argn[32];
100  char qname[512];
101  char argr[512];
102  char *argv[32];
103
104  argcount = q->argc;
105  if (q->type == MR_Q_UPDATE || q->type == MR_Q_APPEND)
106    argcount += q->vcnt;
107
108  switch (argcount)
109    {
110    case 0:
111      sprintf(qname, "   %s, %s ()", q->name, q->shortname);
112      argv[0] = qname;
113      argcount = 1;
114      break;
115
116    case 1:
117      sprintf(qname, "   %s, %s (%s)", q->name, q->shortname, q->fields[0]);
118      argv[0] = qname;
119      argcount = 1;
120      break;
121
122    case 2:
123      sprintf(qname, "   %s, %s (%s, %s)", q->name, q->shortname,
124              q->fields[0], q->fields[1]);
125      argv[0] = qname;
126      argcount = 1;
127      break;
128
129    default:
130      sprintf(qname, "   %s, %s (%s", q->name, q->shortname, q->fields[0]);
131      argv[0] = qname;
132      argcount--;
133      for (i = 1; i < argcount; i++)
134        argv[i] = q->fields[i];
135      sprintf(argn, "%s)", q->fields[argcount]);
136      argv[argcount++] = argn;
137      break;
138    }
139
140  if (q->type == MR_Q_RETRIEVE)
141    {
142      sprintf(argr, "%s => %s", argv[--argcount], q->fields[q->argc]);
143      argv[argcount++] = argr;
144      if (q->vcnt > 1)
145        {
146          for (i = q->argc + 1; i < q->vcnt + q->argc; i++)
147            argv[argcount++] = q->fields[i];
148        }
149    }
150  (*action)(argcount, argv, actarg);
151}
152
153int qcmp(const void *q1, const void *q2)
154{
155  return strcmp((*(struct query **)q1)->name, (*(struct query **)q2)->name);
156}
Note: See TracBrowser for help on using the repository browser.