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