]>
Commit | Line | Data |
---|---|---|
6e6374cb | 1 | /* |
2 | * $Source$ | |
3 | * $Author$ | |
4 | * $Header$ | |
5 | * | |
6 | * Copyright (C) 1987 by the Massachusetts Institute of Technology | |
c801de4c | 7 | * For copying and distribution information, please see the file |
8 | * <mit-copyright.h>. | |
6e6374cb | 9 | * |
6e6374cb | 10 | */ |
11 | ||
12 | #ifndef lint | |
13 | static char *rcsid_test_c = "$Header$"; | |
14 | #endif lint | |
15 | ||
c801de4c | 16 | #include <mit-copyright.h> |
6e6374cb | 17 | #include <stdio.h> |
39de499f | 18 | #include <sys/types.h> |
58b825a4 | 19 | #include <sys/file.h> |
a4fafd85 | 20 | #include <fcntl.h> |
58b825a4 | 21 | #include <ctype.h> |
f071d8a7 | 22 | #include <string.h> |
8defc06b | 23 | #include <moira.h> |
1a5c774c | 24 | #include <com_err.h> |
6e6374cb | 25 | |
58b825a4 | 26 | int recursion = 0; |
1a5c774c | 27 | extern int errno; |
59af7b90 | 28 | extern int sending_version_no; |
1a5c774c | 29 | int count, quit=0; |
30 | char *whoami; | |
31 | ||
32 | #define MAXARGS 20 | |
6e6374cb | 33 | |
6e6374cb | 34 | main(argc, argv) |
35 | int argc; | |
36 | char **argv; | |
6e6374cb | 37 | { |
38 | int status; | |
1a5c774c | 39 | char cmdbuf[BUFSIZ]; |
6e6374cb | 40 | |
6e6374cb | 41 | whoami = argv[0]; |
6e6374cb | 42 | |
365b5cce | 43 | initialize_sms_error_table(); |
44 | initialize_krb_error_table(); | |
6e6374cb | 45 | |
1a5c774c | 46 | while(!quit) { |
47 | printf("moira: "); | |
48 | fflush(stdout); | |
49 | if(!fgets(cmdbuf,BUFSIZ,stdin)) break; | |
50 | execute_line(cmdbuf); | |
6e6374cb | 51 | } |
1a5c774c | 52 | mr_disconnect(); |
53 | exit(0); | |
54 | } | |
55 | ||
56 | execute_line(cmdbuf) | |
57 | char *cmdbuf; | |
58 | { | |
59 | int argc; | |
60 | char *argv[MAXARGS]; | |
61 | ||
62 | argc=parse(cmdbuf, argv); | |
63 | if(argc==0) return; | |
64 | if(!strcmp(argv[0],"noop")) | |
65 | test_noop(); | |
66 | else if(!strcmp(argv[0],"connect") || !strcmp(argv[0],"c")) | |
67 | test_connect(argc, argv); | |
68 | else if(!strcmp(argv[0],"disconnect") || !strcmp(argv[0],"d")) | |
69 | test_disconnect(); | |
70 | else if(!strcmp(argv[0],"host")) | |
71 | test_host(); | |
72 | else if(!strcmp(argv[0],"new") || !strcmp(argv[0],"2")) | |
73 | test_new(); | |
74 | else if(!strcmp(argv[0],"old") || !strcmp(argv[0],"1")) | |
75 | test_old(); | |
76 | else if(!strcmp(argv[0],"motd")) | |
77 | test_motd(); | |
78 | else if(!strcmp(argv[0],"query") || !strcmp(argv[0],"qy")) | |
79 | test_query(argc, argv); | |
80 | else if(!strcmp(argv[0],"auth") || !strcmp(argv[0],"a")) | |
81 | test_auth(argc, argv); | |
82 | else if(!strcmp(argv[0],"access")) | |
83 | test_access(argc, argv); | |
84 | else if(!strcmp(argv[0],"dcm")) | |
85 | test_dcm(); | |
86 | else if(!strcmp(argv[0],"script") || !strcmp(argv[0],"s")) | |
87 | test_script(argc, argv); | |
88 | else if(!strcmp(argv[0],"list_requests") || | |
89 | !strcmp(argv[0],"lr") || !strcmp(argv[0],"?")) | |
90 | test_list_requests(); | |
91 | else if(!strcmp(argv[0],"quit") || !strcmp(argv[0],"Q")) | |
92 | quit=1; | |
93 | else fprintf(stderr, "moira: Unknown request \"%s\"." | |
94 | "Type \"?\" for a request list.\n", argv[0]); | |
95 | } | |
96 | ||
97 | int | |
98 | parse(buf, argv) | |
99 | char *buf, *argv[MAXARGS]; | |
100 | { | |
101 | char *p; | |
102 | int argc, num; | |
103 | ||
104 | for(p=buf, argc=0, argv[0]=buf; *p && *p!='\n'; p++) { | |
105 | if(*p=='"') { | |
106 | char *d=p++; | |
107 | /* skip to close-quote, copying back over open-quote */ | |
108 | while(*p!='"') { | |
109 | if(!*p || *p=='\n') { | |
110 | fprintf(stderr, "moira: Unbalanced quotes in command line\n"); | |
111 | return 0; | |
2c031a2a | 112 | } |
1a5c774c | 113 | if(*p=='\\') { |
114 | if(*++p!='"' && (*p<'0' || *p>'9')) { | |
115 | fprintf(stderr, "moira: Bad use of \\\n"); | |
116 | return 0; | |
117 | } else if (*p!='"') { | |
118 | num=(*p-'0')*64 + (*++p-'0')*8 + (*++p-'0'); | |
119 | *p=num; | |
120 | } | |
6e6374cb | 121 | } |
1a5c774c | 122 | *d++=*p++; |
123 | } | |
124 | if(p==d+1) {*d='\0'; p++;} | |
125 | else while(p>=d) *p--=' '; | |
126 | } | |
127 | if(*p==' ' || *p=='\t') { | |
128 | /* skip whitespace */ | |
129 | for(*p++='\0'; *p==' ' || *p=='\t'; p++); | |
130 | if(*p && *p!='\n') argv[++argc]=p--; | |
131 | } | |
132 | } | |
133 | if(*p=='\n') *p='\0'; | |
134 | return argc+1; | |
6e6374cb | 135 | } |
136 | ||
137 | test_noop() | |
138 | { | |
8defc06b | 139 | int status = mr_noop(); |
1a5c774c | 140 | if (status) com_err("moira (noop)", status, ""); |
6e6374cb | 141 | } |
142 | ||
59af7b90 | 143 | test_new() |
144 | { | |
8defc06b | 145 | sending_version_no = MR_VERSION_2; |
59af7b90 | 146 | } |
147 | ||
148 | test_old() | |
149 | { | |
8defc06b | 150 | sending_version_no = MR_VERSION_1; |
59af7b90 | 151 | } |
152 | ||
011e0a54 | 153 | test_connect(argc, argv) |
154 | int argc; | |
155 | char *argv[]; | |
6e6374cb | 156 | { |
f071d8a7 | 157 | char *server = ""; |
011e0a54 | 158 | int status; |
159 | ||
94551bbc | 160 | if (argc > 1) { |
161 | server = argv[1]; | |
94551bbc | 162 | } |
8defc06b | 163 | status = mr_connect(server); |
1a5c774c | 164 | if (status) com_err("moira (connect)", status, ""); |
6e6374cb | 165 | } |
166 | ||
167 | test_disconnect() | |
168 | { | |
8defc06b | 169 | int status = mr_disconnect(); |
1a5c774c | 170 | if (status) com_err("moira (disconnect)", status, ""); |
6e6374cb | 171 | } |
172 | ||
66848930 | 173 | test_host() |
174 | { | |
175 | char host[BUFSIZ]; | |
176 | int status; | |
177 | ||
f071d8a7 | 178 | memset(host, 0, sizeof(host)); |
66848930 | 179 | |
8defc06b | 180 | if (status = mr_host(host, sizeof(host) - 1)) |
1a5c774c | 181 | com_err("moira (host)", status, ""); |
66848930 | 182 | else |
183 | printf("You are connected to host %s\n", host); | |
184 | } | |
185 | ||
1a5c774c | 186 | test_auth(argc, argv) |
187 | int argc; | |
188 | char *argv[]; | |
6e6374cb | 189 | { |
59af7b90 | 190 | int status; |
191 | ||
8defc06b | 192 | status = mr_auth("mrtest"); |
1a5c774c | 193 | if (status) com_err("moira (auth)", status, ""); |
6e6374cb | 194 | } |
195 | ||
58b825a4 | 196 | test_script(argc, argv) |
197 | int argc; | |
198 | char *argv[]; | |
199 | { | |
200 | FILE *inp; | |
f071d8a7 | 201 | char input[BUFSIZ], *cp; |
58b825a4 | 202 | int status, oldstdout, oldstderr; |
203 | ||
204 | if (recursion > 8) { | |
1a5c774c | 205 | com_err("moira (script)", 0, "too many levels deep in script files\n"); |
58b825a4 | 206 | return; |
207 | } | |
208 | ||
209 | if (argc < 2) { | |
1a5c774c | 210 | com_err("moira (script)", 0, "Usage: script input_file [ output_file ]"); |
58b825a4 | 211 | return; |
212 | } | |
213 | ||
214 | inp = fopen(argv[1], "r"); | |
215 | if (inp == NULL) { | |
bddd8492 | 216 | sprintf(input, "Cannot open input file %s", argv[1]); |
1a5c774c | 217 | com_err("moira (script)", 0, input); |
58b825a4 | 218 | return; |
219 | } | |
220 | ||
221 | if (argc == 3) { | |
222 | printf("Redirecting output to %s\n", argv[2]); | |
223 | fflush(stdout); | |
224 | oldstdout = dup(1); | |
225 | close(1); | |
226 | status = open(argv[2], O_CREAT|O_WRONLY|O_APPEND, 0664); | |
227 | if (status != 1) { | |
228 | close(status); | |
229 | dup2(oldstdout, 1); | |
230 | argc = 2; | |
bddd8492 | 231 | sprintf(input, "Unable to redirect output to %s\n", argv[2]); |
1a5c774c | 232 | com_err("moira (script)", errno, input); |
58b825a4 | 233 | } else { |
234 | fflush(stderr); | |
235 | oldstderr = dup(2); | |
236 | close(2); | |
237 | dup2(1, 2); | |
238 | } | |
239 | } | |
240 | ||
241 | recursion++; | |
242 | ||
243 | for(;;) { | |
244 | if (fgets(input, BUFSIZ, inp) == NULL) | |
245 | break; | |
f071d8a7 | 246 | if ((cp = strchr(input, '\n')) != (char *)NULL) |
58b825a4 | 247 | *cp = 0; |
6a592314 | 248 | if (input[0] == 0) { |
249 | printf("\n"); | |
250 | continue; | |
251 | } | |
58b825a4 | 252 | if (input[0] == '%') { |
253 | for (cp = &input[1]; *cp && isspace(*cp); cp++); | |
254 | printf("Comment: %s\n", cp); | |
255 | continue; | |
256 | } | |
257 | printf("Executing: %s\n", input); | |
1a5c774c | 258 | execute_line(input); |
58b825a4 | 259 | } |
260 | ||
261 | recursion--; | |
262 | ||
263 | fclose(inp); | |
264 | if (argc == 3) { | |
265 | fflush(stdout); | |
266 | close(1); | |
267 | dup2(oldstdout, 1); | |
268 | close(oldstdout); | |
269 | fflush(stderr); | |
270 | close(2); | |
271 | dup2(oldstderr, 2); | |
272 | close(oldstderr); | |
273 | } | |
274 | } | |
275 | ||
6e6374cb | 276 | print_reply(argc, argv) |
277 | int argc; | |
278 | char **argv; | |
279 | { | |
280 | int i; | |
281 | for (i = 0; i < argc; i++) { | |
282 | if (i != 0) printf(", "); | |
283 | printf("%s", argv[i]); | |
284 | } | |
285 | printf("\n"); | |
286 | count++; | |
8defc06b | 287 | return(MR_CONT); |
6e6374cb | 288 | } |
289 | ||
290 | test_query(argc, argv) | |
291 | int argc; | |
292 | char **argv; | |
293 | { | |
294 | int status; | |
295 | if (argc < 2) { | |
1a5c774c | 296 | com_err("moira (query)", 0, "Usage: query handle [ args ... ]"); |
6e6374cb | 297 | return; |
298 | } | |
59af7b90 | 299 | |
6e6374cb | 300 | count = 0; |
8defc06b | 301 | status = mr_query(argv[1], argc-2, argv+2, print_reply, (char *)NULL); |
303cb414 | 302 | printf("%d tuple%s\n", count, ((count == 1) ? "" : "s")); |
1a5c774c | 303 | if (status) com_err("moira (query)", status, ""); |
6e6374cb | 304 | } |
305 | ||
306 | test_access(argc, argv) | |
307 | int argc; | |
308 | char **argv; | |
309 | { | |
310 | int status; | |
311 | if (argc < 2) { | |
1a5c774c | 312 | com_err("moira (access)", 0, "Usage: access handle [ args ... ]"); |
6e6374cb | 313 | return; |
314 | } | |
8defc06b | 315 | status = mr_access(argv[1], argc-2, argv+2); |
1a5c774c | 316 | if (status) com_err("moira (access)", status, ""); |
6e6374cb | 317 | } |
fc3ac28e | 318 | |
319 | ||
320 | test_dcm(argc, argv) | |
321 | int argc; | |
322 | char **argv; | |
323 | { | |
324 | int status; | |
325 | ||
8defc06b | 326 | if (status = mr_do_update()) |
1a5c774c | 327 | com_err("moira (dcm)", status, " while triggering dcm"); |
fc3ac28e | 328 | } |
093b57b1 | 329 | |
330 | ||
331 | test_motd(argc, argv) | |
332 | int argc; | |
333 | char **argv; | |
334 | { | |
335 | int status; | |
336 | char *motd; | |
337 | ||
8defc06b | 338 | if (status = mr_motd(&motd)) |
1a5c774c | 339 | com_err("moira (motd)", status, " while getting motd"); |
093b57b1 | 340 | if (motd) |
341 | printf("%s\n", motd); | |
342 | else | |
343 | printf("No message of the day.\n"); | |
344 | } | |
1a5c774c | 345 | |
346 | test_list_requests() | |
347 | { | |
348 | printf("Available moira requests:\n"); | |
349 | printf("\n"); | |
350 | printf("noop\t\t\tAsk Moira to do nothing\n"); | |
351 | printf("connect, c\t\tConnect to Moira server\n"); | |
352 | printf("disconnect, d\t\tDisconnect from server\n"); | |
353 | printf("host\t\t\tIdentify the server host\n"); | |
354 | printf("new, 2\t\t\tUse new protocol\n"); | |
355 | printf("old, 1\t\t\tUse old protocol\n"); | |
356 | printf("motd, m\t\t\tGet the Message of the Day\n"); | |
357 | printf("query, qy\t\tMake a query.\n"); | |
358 | printf("auth, a\t\t\tAuthenticate to Moira.\n"); | |
359 | printf("access\t\t\tCheck access to a Moira query.\n"); | |
360 | printf("dcm\t\t\tTrigger the DCM\n"); | |
361 | printf("script, s\t\tRead commands from a script.\n"); | |
362 | printf("list_requests, lr, ?\tList available commands.\n"); | |
363 | printf("quit, Q\t\t\tLeave the subsystem.\n"); | |
364 | } |