]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * $Source$ | |
3 | * $Author$ | |
4 | * $Header$ | |
5 | * | |
6 | * Copyright (C) 1987 by the Massachusetts Institute of Technology | |
7 | * For copying and distribution information, please see the file | |
8 | * <mit-copyright.h>. | |
9 | * | |
10 | */ | |
11 | ||
12 | #ifndef lint | |
13 | static char *rcsid_qsubs_c = "$Header$"; | |
14 | #endif lint | |
15 | ||
16 | #include <mit-copyright.h> | |
17 | #include <sms.h> | |
18 | #include "query.h" | |
19 | ||
20 | extern struct query Queries1[], Queries2[]; | |
21 | extern int QueryCount1, QueryCount2; | |
22 | ||
23 | struct query * | |
24 | get_query_by_name(name, version) | |
25 | register char *name; | |
26 | int version; | |
27 | { | |
28 | register struct query *q; | |
29 | register int i; | |
30 | ||
31 | if (version == SMS_VERSION_1) { | |
32 | q = Queries1; | |
33 | i = QueryCount1; | |
34 | } else { | |
35 | q = Queries2; | |
36 | i = QueryCount2; | |
37 | } | |
38 | ||
39 | if (strlen(name) == 4) { | |
40 | while (--i >= 0) { | |
41 | if (!strcmp(q->shortname, name)) return(q); | |
42 | q++; | |
43 | } | |
44 | } else { | |
45 | while (--i >= 0) { | |
46 | if (!strcmp(q->name, name)) return(q); | |
47 | q++; | |
48 | } | |
49 | } | |
50 | ||
51 | return((struct query *)0); | |
52 | } | |
53 | ||
54 | list_queries(version, action, actarg) | |
55 | int version; | |
56 | int (*action)(); | |
57 | int actarg; | |
58 | { | |
59 | register struct query *q; | |
60 | register int i; | |
61 | static struct query **squeries1 = (struct query **)0; | |
62 | static struct query **squeries2 = (struct query **)0; | |
63 | register struct query **sq; | |
64 | char qnames[80]; | |
65 | char *qnp; | |
66 | int count; | |
67 | int qcmp(); | |
68 | ||
69 | if (version == SMS_VERSION_1) { | |
70 | count = QueryCount1; | |
71 | if (squeries1 == (struct query **)0) { | |
72 | sq = (struct query **)malloc(count * sizeof (struct query *)); | |
73 | squeries1 = sq; | |
74 | q = Queries1; | |
75 | for (i = count; --i >= 0; ) | |
76 | *sq++ = q++; | |
77 | qsort(squeries1, count, sizeof (struct query *), qcmp); | |
78 | } | |
79 | sq = squeries1; | |
80 | } else { | |
81 | count = QueryCount2; | |
82 | if (squeries2 == (struct query **)0) { | |
83 | sq = (struct query **)malloc(count * sizeof (struct query *)); | |
84 | squeries2 = sq; | |
85 | q = Queries2; | |
86 | for (i = count; --i >= 0; ) | |
87 | *sq++ = q++; | |
88 | qsort(squeries2, count, sizeof (struct query *), qcmp); | |
89 | } | |
90 | sq = squeries2; | |
91 | } | |
92 | ||
93 | qnp = qnames; | |
94 | for (i = count; --i >= 0; sq++) { | |
95 | sprintf(qnames, "%s (%s)", (*sq)->name, (*sq)->shortname); | |
96 | (*action)(1, &qnp, actarg); | |
97 | } | |
98 | strcpy(qnames, "_help"); | |
99 | (*action)(1, &qnp, actarg); | |
100 | strcpy(qnames, "_list_queries"); | |
101 | (*action)(1, &qnp, actarg); | |
102 | strcpy(qnames, "_list_users"); | |
103 | (*action)(1, &qnp, actarg); | |
104 | } | |
105 | ||
106 | help_query(q, action, actarg) | |
107 | register struct query *q; | |
108 | int (*action)(); | |
109 | int actarg; | |
110 | { | |
111 | register int argcount; | |
112 | register int i; | |
113 | char argn[32]; | |
114 | char qname[512]; | |
115 | char argr[512]; | |
116 | char *argv[32]; | |
117 | ||
118 | argcount = q->argc; | |
119 | if (q->type == UPDATE || q->type == APPEND) argcount += q->vcnt; | |
120 | ||
121 | switch (argcount) { | |
122 | case 0: | |
123 | sprintf(qname, " %s, %s ()", q->name, q->shortname); | |
124 | argv[0] = qname; | |
125 | argcount = 1; | |
126 | break; | |
127 | ||
128 | case 1: | |
129 | sprintf(qname, " %s, %s (%s)", q->name, q->shortname, q->fields[0]); | |
130 | argv[0] = qname; | |
131 | argcount = 1; | |
132 | break; | |
133 | ||
134 | case 2: | |
135 | sprintf(qname, " %s, %s (%s, %s)", q->name, q->shortname, | |
136 | q->fields[0], q->fields[1]); | |
137 | argv[0] = qname; | |
138 | argcount = 1; | |
139 | break; | |
140 | ||
141 | default: | |
142 | sprintf(qname, " %s, %s (%s", q->name, q->shortname, q->fields[0]); | |
143 | argv[0] = qname; | |
144 | argcount--; | |
145 | for (i = 1; i < argcount; i++) argv[i] = q->fields[i]; | |
146 | sprintf(argn, "%s)", q->fields[argcount]); | |
147 | argv[argcount++] = argn; | |
148 | break; | |
149 | } | |
150 | if (q->type == RETRIEVE) { | |
151 | sprintf(argr, "%s => %s", argv[--argcount], q->fields[q->argc]); | |
152 | argv[argcount++] = argr; | |
153 | if (q->vcnt > 1) | |
154 | for (i = q->argc + 1; i < q->vcnt + q->argc; i++) | |
155 | argv[argcount++] = q->fields[i]; | |
156 | } | |
157 | (*action)(argcount, argv, actarg); | |
158 | } | |
159 | ||
160 | qcmp(q1, q2) | |
161 | struct query **q1; | |
162 | struct query **q2; | |
163 | { | |
164 | return(strcmp((*q1)->name, (*q2)->name)); | |
165 | } |