6 * Copyright (C) 1987 by the Massachusetts Institute of Technology
7 * For copying and distribution information, please see the file
13 static char *rcsid_test_c = "$Header$";
16 #include <mit-copyright.h>
18 #include <sys/types.h>
32 int recursion = 0, interactive;
34 extern int sending_version_no;
35 int count, quit=0, cancel=0;
58 struct sigaction action;
62 interactive = (isatty(0) && isatty(1));
64 initialize_sms_error_table();
65 initialize_krb_error_table();
68 /* we don't want filename completion */
69 rl_bind_key ('\t', rl_insert);
73 action.sa_handler = discard_input;
75 sigemptyset(&action.sa_mask);
76 sigaction(SIGINT, &action, NULL);
79 signal(SIGINT, discard_input);
84 if(!mr_gets("moira: ",cmdbuf,BUFSIZ)) break;
100 /* if we're inside a script, we have to clean up file descriptors,
101 so don't jump out yet */
113 char *mr_gets(prompt, buf, len)
122 if (!in) return NULL;
126 strncpy(buf, in, len-1);
133 printf("%s", prompt);
135 in=fgets(buf, len, stdin);
137 if(strchr(buf,'\n')) *(strchr(buf,'\n'))=0;
147 argc=parse(cmdbuf, argv);
149 if(!strcmp(argv[0],"noop"))
151 else if(!strcmp(argv[0],"connect") || !strcmp(argv[0],"c"))
152 test_connect(argc, argv);
153 else if(!strcmp(argv[0],"disconnect") || !strcmp(argv[0],"d"))
155 else if(!strcmp(argv[0],"host"))
157 else if(!strcmp(argv[0],"new") || !strcmp(argv[0],"2"))
159 else if(!strcmp(argv[0],"old") || !strcmp(argv[0],"1"))
161 else if(!strcmp(argv[0],"motd"))
163 else if(!strcmp(argv[0],"query") || !strcmp(argv[0],"qy"))
164 test_query(argc, argv);
165 else if(!strcmp(argv[0],"auth") || !strcmp(argv[0],"a"))
166 test_auth(argc, argv);
167 else if(!strcmp(argv[0],"access"))
168 test_access(argc, argv);
169 else if(!strcmp(argv[0],"dcm"))
171 else if(!strcmp(argv[0],"script") || !strcmp(argv[0],"s"))
172 test_script(argc, argv);
173 else if(!strcmp(argv[0],"list_requests") ||
174 !strcmp(argv[0],"lr") || !strcmp(argv[0],"?"))
175 test_list_requests();
176 else if(!strcmp(argv[0],"quit") || !strcmp(argv[0],"Q"))
178 else fprintf(stderr, "moira: Unknown request \"%s\". Type \"?\" for a request list.\n", argv[0]);
183 char *buf, *argv[MAXARGS];
190 for(p=buf, argc=0, argv[0]=buf; *p && *p!='\n'; p++) {
193 /* skip to close-quote, copying back over open-quote */
195 if(!*p || *p=='\n') {
196 fprintf(stderr, "moira: Unbalanced quotes in command line\n");
200 if(*++p!='"' && (*p<'0' || *p>'9') && (*p!='\\')) {
201 fprintf(stderr, "moira: Bad use of \\\n");
203 } else if (*p>='0' && *p<='9') {
204 num=(*p-'0')*64 + (*++p-'0')*8 + (*++p-'0');
210 if(p==d+1) {*d='\0'; p++;}
211 else while(p>=d) *p--=' ';
213 if(*p==' ' || *p=='\t') {
214 /* skip whitespace */
215 for(*p++='\0'; *p==' ' || *p=='\t'; p++);
216 if(*p && *p!='\n') argv[++argc]=p--;
219 if(*p=='\n') *p='\0';
225 int status = mr_noop();
226 if (status) com_err("moira (noop)", status, "");
231 sending_version_no = MR_VERSION_2;
236 sending_version_no = MR_VERSION_1;
239 test_connect(argc, argv)
249 status = mr_connect(server);
250 if (status) com_err("moira (connect)", status, "");
255 int status = mr_disconnect();
256 if (status) com_err("moira (disconnect)", status, "");
264 memset(host, 0, sizeof(host));
266 if (status = mr_host(host, sizeof(host) - 1))
267 com_err("moira (host)", status, "");
269 printf("You are connected to host %s\n", host);
272 test_auth(argc, argv)
278 status = mr_auth("mrtest");
279 if (status) com_err("moira (auth)", status, "");
282 test_script(argc, argv)
287 char input[BUFSIZ], *cp;
288 int status, oldstdout, oldstderr;
291 com_err("moira (script)", 0, "too many levels deep in script files\n");
296 com_err("moira (script)", 0, "Usage: script input_file [ output_file ]");
300 inp = fopen(argv[1], "r");
302 sprintf(input, "Cannot open input file %s", argv[1]);
303 com_err("moira (script)", 0, input);
308 printf("Redirecting output to %s\n", argv[2]);
312 status = open(argv[2], O_CREAT|O_WRONLY|O_APPEND, 0664);
317 sprintf(input, "Unable to redirect output to %s\n", argv[2]);
318 com_err("moira (script)", errno, input);
330 if (fgets(input, BUFSIZ, inp) == NULL)
332 if ((cp = strchr(input, '\n')) != (char *)NULL)
338 if (input[0] == '%') {
339 for (cp = &input[1]; *cp && isspace(*cp); cp++);
340 printf("Comment: %s\n", cp);
343 printf("Executing: %s\n", input);
348 if(!recursion) cancel=0;
363 print_reply(argc, argv)
368 for (i = 0; i < argc; i++) {
369 if (i != 0) printf(", ");
370 printf("%s", argv[i]);
377 test_query(argc, argv)
389 com_err("moira (query)", 0, "Usage: query handle [ args ... ]");
394 /* Don't allow ^C during the query: it will confuse libmoira's
395 internal state. (Yay static variables) */
398 sigaddset(&sigs, SIGINT);
399 sigprocmask(SIG_BLOCK, &sigs, NULL);
401 mask = sigblock(SIGINT);
403 status = mr_query(argv[1], argc-2, argv+2, print_reply, (char *)NULL);
405 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
409 printf("%d tuple%s\n", count, ((count == 1) ? "" : "s"));
410 if (status) com_err("moira (query)", status, "");
413 test_access(argc, argv)
419 com_err("moira (access)", 0, "Usage: access handle [ args ... ]");
422 status = mr_access(argv[1], argc-2, argv+2);
423 if (status) com_err("moira (access)", status, "");
433 if (status = mr_do_update())
434 com_err("moira (dcm)", status, " while triggering dcm");
438 test_motd(argc, argv)
445 if (status = mr_motd(&motd))
446 com_err("moira (motd)", status, " while getting motd");
448 printf("%s\n", motd);
450 printf("No message of the day.\n");
455 printf("Available moira requests:\n");
457 printf("noop\t\t\tAsk Moira to do nothing\n");
458 printf("connect, c\t\tConnect to Moira server\n");
459 printf("disconnect, d\t\tDisconnect from server\n");
460 printf("host\t\t\tIdentify the server host\n");
461 printf("new, 2\t\t\tUse new protocol\n");
462 printf("old, 1\t\t\tUse old protocol\n");
463 printf("motd, m\t\t\tGet the Message of the Day\n");
464 printf("query, qy\t\tMake a query.\n");
465 printf("auth, a\t\t\tAuthenticate to Moira.\n");
466 printf("access\t\t\tCheck access to a Moira query.\n");
467 printf("dcm\t\t\tTrigger the DCM\n");
468 printf("script, s\t\tRead commands from a script.\n");
469 printf("list_requests, lr, ?\tList available commands.\n");
470 printf("quit, Q\t\t\tLeave the subsystem.\n");