]>
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$"; | |
74c84b52 | 14 | #endif /* lint */ |
6e6374cb | 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> |
f281aa29 | 25 | #include <setjmp.h> |
26 | #include <signal.h> | |
27 | ||
28 | #ifdef USE_READLINE | |
29 | #include "readline.h" | |
30 | #endif | |
6e6374cb | 31 | |
58b825a4 | 32 | int recursion = 0; |
1a5c774c | 33 | extern int errno; |
59af7b90 | 34 | extern int sending_version_no; |
1a5c774c | 35 | int count, quit=0; |
36 | char *whoami; | |
74c84b52 | 37 | #ifdef POSIX |
38 | sigjmp_buf jb; | |
39 | #else | |
f281aa29 | 40 | jmp_buf jb; |
74c84b52 | 41 | #endif |
1a5c774c | 42 | |
43 | #define MAXARGS 20 | |
6e6374cb | 44 | |
74c84b52 | 45 | void discard_input(void); |
f281aa29 | 46 | char *mr_gets(char *, char *, size_t); |
47 | ||
6e6374cb | 48 | main(argc, argv) |
49 | int argc; | |
50 | char **argv; | |
6e6374cb | 51 | { |
52 | int status; | |
1a5c774c | 53 | char cmdbuf[BUFSIZ]; |
f281aa29 | 54 | #ifdef POSIX |
55 | struct sigaction action; | |
56 | #endif | |
6e6374cb | 57 | |
6e6374cb | 58 | whoami = argv[0]; |
6e6374cb | 59 | |
365b5cce | 60 | initialize_sms_error_table(); |
61 | initialize_krb_error_table(); | |
6e6374cb | 62 | |
3442308f | 63 | #ifdef USE_READLINE |
64 | /* we don't want filename completion */ | |
65 | rl_bind_key ('\t', rl_insert); | |
66 | #endif | |
67 | ||
f281aa29 | 68 | #ifdef POSIX |
69 | action.sa_handler = discard_input; | |
70 | action.sa_flags = 0; | |
71 | sigemptyset(&action.sa_mask); | |
72 | sigaction(SIGINT, &action, NULL); | |
74c84b52 | 73 | sigsetjmp(jb, 1); |
f281aa29 | 74 | #else |
75 | signal(SIGINT, discard_input); | |
f281aa29 | 76 | setjmp(jb); |
74c84b52 | 77 | #endif |
f281aa29 | 78 | |
1a5c774c | 79 | while(!quit) { |
f281aa29 | 80 | if(!mr_gets("moira: ",cmdbuf,BUFSIZ)) break; |
1a5c774c | 81 | execute_line(cmdbuf); |
6e6374cb | 82 | } |
1a5c774c | 83 | mr_disconnect(); |
84 | exit(0); | |
85 | } | |
86 | ||
74c84b52 | 87 | void discard_input(void) |
f281aa29 | 88 | { |
89 | putc('\n', stdout); | |
74c84b52 | 90 | #ifdef POSIX |
91 | siglongjmp(jb, 1); | |
92 | #else | |
f281aa29 | 93 | longjmp(jb, 1); |
74c84b52 | 94 | #endif |
f281aa29 | 95 | } |
96 | ||
97 | char *mr_gets(char *prompt, char *buf, size_t len) | |
98 | { | |
99 | char *in; | |
100 | #ifdef USE_READLINE | |
9dbe69dd | 101 | if(isatty(0) && isatty(1)) { |
f281aa29 | 102 | in=readline(prompt); |
103 | ||
104 | if (!in) return NULL; | |
105 | if (*in) { | |
106 | add_history(in); | |
f281aa29 | 107 | } |
74c84b52 | 108 | strncpy(buf, in, len-1); |
109 | buf[len]=0; | |
3442308f | 110 | free(in); |
f281aa29 | 111 | |
112 | return buf; | |
113 | } | |
114 | #endif | |
115 | printf("%s", prompt); | |
116 | fflush(stdout); | |
117 | in=fgets(buf, len, stdin); | |
118 | if(!in) return in; | |
119 | if(strchr(buf,'\n')) *(strchr(buf,'\n'))=0; | |
120 | return buf; | |
121 | } | |
122 | ||
1a5c774c | 123 | execute_line(cmdbuf) |
124 | char *cmdbuf; | |
125 | { | |
126 | int argc; | |
127 | char *argv[MAXARGS]; | |
128 | ||
129 | argc=parse(cmdbuf, argv); | |
130 | if(argc==0) return; | |
131 | if(!strcmp(argv[0],"noop")) | |
132 | test_noop(); | |
133 | else if(!strcmp(argv[0],"connect") || !strcmp(argv[0],"c")) | |
134 | test_connect(argc, argv); | |
135 | else if(!strcmp(argv[0],"disconnect") || !strcmp(argv[0],"d")) | |
136 | test_disconnect(); | |
137 | else if(!strcmp(argv[0],"host")) | |
138 | test_host(); | |
139 | else if(!strcmp(argv[0],"new") || !strcmp(argv[0],"2")) | |
140 | test_new(); | |
141 | else if(!strcmp(argv[0],"old") || !strcmp(argv[0],"1")) | |
142 | test_old(); | |
143 | else if(!strcmp(argv[0],"motd")) | |
144 | test_motd(); | |
145 | else if(!strcmp(argv[0],"query") || !strcmp(argv[0],"qy")) | |
146 | test_query(argc, argv); | |
147 | else if(!strcmp(argv[0],"auth") || !strcmp(argv[0],"a")) | |
148 | test_auth(argc, argv); | |
149 | else if(!strcmp(argv[0],"access")) | |
150 | test_access(argc, argv); | |
151 | else if(!strcmp(argv[0],"dcm")) | |
152 | test_dcm(); | |
153 | else if(!strcmp(argv[0],"script") || !strcmp(argv[0],"s")) | |
154 | test_script(argc, argv); | |
155 | else if(!strcmp(argv[0],"list_requests") || | |
156 | !strcmp(argv[0],"lr") || !strcmp(argv[0],"?")) | |
157 | test_list_requests(); | |
158 | else if(!strcmp(argv[0],"quit") || !strcmp(argv[0],"Q")) | |
159 | quit=1; | |
125f757c | 160 | else fprintf(stderr, "moira: Unknown request \"%s\". Type \"?\" for a request list.\n", argv[0]); |
1a5c774c | 161 | } |
162 | ||
163 | int | |
164 | parse(buf, argv) | |
165 | char *buf, *argv[MAXARGS]; | |
166 | { | |
167 | char *p; | |
168 | int argc, num; | |
f281aa29 | 169 | |
170 | if(!*buf) return 0; | |
171 | ||
1a5c774c | 172 | for(p=buf, argc=0, argv[0]=buf; *p && *p!='\n'; p++) { |
173 | if(*p=='"') { | |
174 | char *d=p++; | |
175 | /* skip to close-quote, copying back over open-quote */ | |
176 | while(*p!='"') { | |
177 | if(!*p || *p=='\n') { | |
178 | fprintf(stderr, "moira: Unbalanced quotes in command line\n"); | |
179 | return 0; | |
2c031a2a | 180 | } |
1a5c774c | 181 | if(*p=='\\') { |
f281aa29 | 182 | if(*++p!='"' && (*p<'0' || *p>'9') && (*p!='\\')) { |
1a5c774c | 183 | fprintf(stderr, "moira: Bad use of \\\n"); |
184 | return 0; | |
f281aa29 | 185 | } else if (*p>='0' && *p<='9') { |
1a5c774c | 186 | num=(*p-'0')*64 + (*++p-'0')*8 + (*++p-'0'); |
187 | *p=num; | |
188 | } | |
6e6374cb | 189 | } |
1a5c774c | 190 | *d++=*p++; |
191 | } | |
192 | if(p==d+1) {*d='\0'; p++;} | |
193 | else while(p>=d) *p--=' '; | |
194 | } | |
195 | if(*p==' ' || *p=='\t') { | |
196 | /* skip whitespace */ | |
197 | for(*p++='\0'; *p==' ' || *p=='\t'; p++); | |
198 | if(*p && *p!='\n') argv[++argc]=p--; | |
199 | } | |
200 | } | |
201 | if(*p=='\n') *p='\0'; | |
202 | return argc+1; | |
6e6374cb | 203 | } |
204 | ||
205 | test_noop() | |
206 | { | |
8defc06b | 207 | int status = mr_noop(); |
1a5c774c | 208 | if (status) com_err("moira (noop)", status, ""); |
6e6374cb | 209 | } |
210 | ||
59af7b90 | 211 | test_new() |
212 | { | |
8defc06b | 213 | sending_version_no = MR_VERSION_2; |
59af7b90 | 214 | } |
215 | ||
216 | test_old() | |
217 | { | |
8defc06b | 218 | sending_version_no = MR_VERSION_1; |
59af7b90 | 219 | } |
220 | ||
011e0a54 | 221 | test_connect(argc, argv) |
222 | int argc; | |
223 | char *argv[]; | |
6e6374cb | 224 | { |
f071d8a7 | 225 | char *server = ""; |
011e0a54 | 226 | int status; |
227 | ||
94551bbc | 228 | if (argc > 1) { |
229 | server = argv[1]; | |
94551bbc | 230 | } |
8defc06b | 231 | status = mr_connect(server); |
1a5c774c | 232 | if (status) com_err("moira (connect)", status, ""); |
6e6374cb | 233 | } |
234 | ||
235 | test_disconnect() | |
236 | { | |
8defc06b | 237 | int status = mr_disconnect(); |
1a5c774c | 238 | if (status) com_err("moira (disconnect)", status, ""); |
6e6374cb | 239 | } |
240 | ||
66848930 | 241 | test_host() |
242 | { | |
243 | char host[BUFSIZ]; | |
244 | int status; | |
245 | ||
f071d8a7 | 246 | memset(host, 0, sizeof(host)); |
66848930 | 247 | |
8defc06b | 248 | if (status = mr_host(host, sizeof(host) - 1)) |
1a5c774c | 249 | com_err("moira (host)", status, ""); |
66848930 | 250 | else |
251 | printf("You are connected to host %s\n", host); | |
252 | } | |
253 | ||
1a5c774c | 254 | test_auth(argc, argv) |
255 | int argc; | |
256 | char *argv[]; | |
6e6374cb | 257 | { |
59af7b90 | 258 | int status; |
259 | ||
8defc06b | 260 | status = mr_auth("mrtest"); |
1a5c774c | 261 | if (status) com_err("moira (auth)", status, ""); |
6e6374cb | 262 | } |
263 | ||
58b825a4 | 264 | test_script(argc, argv) |
265 | int argc; | |
266 | char *argv[]; | |
267 | { | |
268 | FILE *inp; | |
f071d8a7 | 269 | char input[BUFSIZ], *cp; |
58b825a4 | 270 | int status, oldstdout, oldstderr; |
271 | ||
272 | if (recursion > 8) { | |
1a5c774c | 273 | com_err("moira (script)", 0, "too many levels deep in script files\n"); |
58b825a4 | 274 | return; |
275 | } | |
276 | ||
277 | if (argc < 2) { | |
1a5c774c | 278 | com_err("moira (script)", 0, "Usage: script input_file [ output_file ]"); |
58b825a4 | 279 | return; |
280 | } | |
281 | ||
282 | inp = fopen(argv[1], "r"); | |
283 | if (inp == NULL) { | |
bddd8492 | 284 | sprintf(input, "Cannot open input file %s", argv[1]); |
1a5c774c | 285 | com_err("moira (script)", 0, input); |
58b825a4 | 286 | return; |
287 | } | |
288 | ||
289 | if (argc == 3) { | |
290 | printf("Redirecting output to %s\n", argv[2]); | |
291 | fflush(stdout); | |
292 | oldstdout = dup(1); | |
293 | close(1); | |
294 | status = open(argv[2], O_CREAT|O_WRONLY|O_APPEND, 0664); | |
295 | if (status != 1) { | |
296 | close(status); | |
297 | dup2(oldstdout, 1); | |
298 | argc = 2; | |
bddd8492 | 299 | sprintf(input, "Unable to redirect output to %s\n", argv[2]); |
1a5c774c | 300 | com_err("moira (script)", errno, input); |
58b825a4 | 301 | } else { |
302 | fflush(stderr); | |
303 | oldstderr = dup(2); | |
304 | close(2); | |
305 | dup2(1, 2); | |
306 | } | |
307 | } | |
308 | ||
309 | recursion++; | |
310 | ||
311 | for(;;) { | |
312 | if (fgets(input, BUFSIZ, inp) == NULL) | |
313 | break; | |
f071d8a7 | 314 | if ((cp = strchr(input, '\n')) != (char *)NULL) |
58b825a4 | 315 | *cp = 0; |
6a592314 | 316 | if (input[0] == 0) { |
317 | printf("\n"); | |
318 | continue; | |
319 | } | |
58b825a4 | 320 | if (input[0] == '%') { |
321 | for (cp = &input[1]; *cp && isspace(*cp); cp++); | |
322 | printf("Comment: %s\n", cp); | |
323 | continue; | |
324 | } | |
325 | printf("Executing: %s\n", input); | |
1a5c774c | 326 | execute_line(input); |
58b825a4 | 327 | } |
328 | ||
329 | recursion--; | |
330 | ||
331 | fclose(inp); | |
332 | if (argc == 3) { | |
333 | fflush(stdout); | |
334 | close(1); | |
335 | dup2(oldstdout, 1); | |
336 | close(oldstdout); | |
337 | fflush(stderr); | |
338 | close(2); | |
339 | dup2(oldstderr, 2); | |
340 | close(oldstderr); | |
341 | } | |
342 | } | |
343 | ||
6e6374cb | 344 | print_reply(argc, argv) |
345 | int argc; | |
346 | char **argv; | |
347 | { | |
348 | int i; | |
349 | for (i = 0; i < argc; i++) { | |
350 | if (i != 0) printf(", "); | |
351 | printf("%s", argv[i]); | |
352 | } | |
353 | printf("\n"); | |
354 | count++; | |
8defc06b | 355 | return(MR_CONT); |
6e6374cb | 356 | } |
357 | ||
358 | test_query(argc, argv) | |
359 | int argc; | |
360 | char **argv; | |
361 | { | |
362 | int status; | |
363 | if (argc < 2) { | |
1a5c774c | 364 | com_err("moira (query)", 0, "Usage: query handle [ args ... ]"); |
6e6374cb | 365 | return; |
366 | } | |
59af7b90 | 367 | |
6e6374cb | 368 | count = 0; |
8defc06b | 369 | status = mr_query(argv[1], argc-2, argv+2, print_reply, (char *)NULL); |
303cb414 | 370 | printf("%d tuple%s\n", count, ((count == 1) ? "" : "s")); |
1a5c774c | 371 | if (status) com_err("moira (query)", status, ""); |
6e6374cb | 372 | } |
373 | ||
374 | test_access(argc, argv) | |
375 | int argc; | |
376 | char **argv; | |
377 | { | |
378 | int status; | |
379 | if (argc < 2) { | |
1a5c774c | 380 | com_err("moira (access)", 0, "Usage: access handle [ args ... ]"); |
6e6374cb | 381 | return; |
382 | } | |
8defc06b | 383 | status = mr_access(argv[1], argc-2, argv+2); |
1a5c774c | 384 | if (status) com_err("moira (access)", status, ""); |
6e6374cb | 385 | } |
fc3ac28e | 386 | |
387 | ||
388 | test_dcm(argc, argv) | |
389 | int argc; | |
390 | char **argv; | |
391 | { | |
392 | int status; | |
393 | ||
8defc06b | 394 | if (status = mr_do_update()) |
1a5c774c | 395 | com_err("moira (dcm)", status, " while triggering dcm"); |
fc3ac28e | 396 | } |
093b57b1 | 397 | |
398 | ||
399 | test_motd(argc, argv) | |
400 | int argc; | |
401 | char **argv; | |
402 | { | |
403 | int status; | |
404 | char *motd; | |
405 | ||
8defc06b | 406 | if (status = mr_motd(&motd)) |
1a5c774c | 407 | com_err("moira (motd)", status, " while getting motd"); |
093b57b1 | 408 | if (motd) |
409 | printf("%s\n", motd); | |
410 | else | |
411 | printf("No message of the day.\n"); | |
412 | } | |
1a5c774c | 413 | |
414 | test_list_requests() | |
415 | { | |
416 | printf("Available moira requests:\n"); | |
417 | printf("\n"); | |
418 | printf("noop\t\t\tAsk Moira to do nothing\n"); | |
419 | printf("connect, c\t\tConnect to Moira server\n"); | |
420 | printf("disconnect, d\t\tDisconnect from server\n"); | |
421 | printf("host\t\t\tIdentify the server host\n"); | |
422 | printf("new, 2\t\t\tUse new protocol\n"); | |
423 | printf("old, 1\t\t\tUse old protocol\n"); | |
424 | printf("motd, m\t\t\tGet the Message of the Day\n"); | |
425 | printf("query, qy\t\tMake a query.\n"); | |
426 | printf("auth, a\t\t\tAuthenticate to Moira.\n"); | |
427 | printf("access\t\t\tCheck access to a Moira query.\n"); | |
428 | printf("dcm\t\t\tTrigger the DCM\n"); | |
429 | printf("script, s\t\tRead commands from a script.\n"); | |
430 | printf("list_requests, lr, ?\tList available commands.\n"); | |
431 | printf("quit, Q\t\t\tLeave the subsystem.\n"); | |
432 | } |