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>
34 extern int sending_version_no;
45 void discard_input(void);
46 char *mr_gets(char *, char *, size_t);
55 struct sigaction action;
60 initialize_sms_error_table();
61 initialize_krb_error_table();
64 action.sa_handler = discard_input;
66 sigemptyset(&action.sa_mask);
67 sigaction(SIGINT, &action, NULL);
70 signal(SIGINT, discard_input);
75 if(!mr_gets("moira: ",cmdbuf,BUFSIZ)) break;
82 void discard_input(void)
92 char *mr_gets(char *prompt, char *buf, size_t len)
103 strncpy(buf, in, len-1);
109 printf("%s", prompt);
111 in=fgets(buf, len, stdin);
113 if(strchr(buf,'\n')) *(strchr(buf,'\n'))=0;
123 argc=parse(cmdbuf, argv);
125 if(!strcmp(argv[0],"noop"))
127 else if(!strcmp(argv[0],"connect") || !strcmp(argv[0],"c"))
128 test_connect(argc, argv);
129 else if(!strcmp(argv[0],"disconnect") || !strcmp(argv[0],"d"))
131 else if(!strcmp(argv[0],"host"))
133 else if(!strcmp(argv[0],"new") || !strcmp(argv[0],"2"))
135 else if(!strcmp(argv[0],"old") || !strcmp(argv[0],"1"))
137 else if(!strcmp(argv[0],"motd"))
139 else if(!strcmp(argv[0],"query") || !strcmp(argv[0],"qy"))
140 test_query(argc, argv);
141 else if(!strcmp(argv[0],"auth") || !strcmp(argv[0],"a"))
142 test_auth(argc, argv);
143 else if(!strcmp(argv[0],"access"))
144 test_access(argc, argv);
145 else if(!strcmp(argv[0],"dcm"))
147 else if(!strcmp(argv[0],"script") || !strcmp(argv[0],"s"))
148 test_script(argc, argv);
149 else if(!strcmp(argv[0],"list_requests") ||
150 !strcmp(argv[0],"lr") || !strcmp(argv[0],"?"))
151 test_list_requests();
152 else if(!strcmp(argv[0],"quit") || !strcmp(argv[0],"Q"))
154 else fprintf(stderr, "moira: Unknown request \"%s\". Type \"?\" for a request list.\n", argv[0]);
159 char *buf, *argv[MAXARGS];
166 for(p=buf, argc=0, argv[0]=buf; *p && *p!='\n'; p++) {
169 /* skip to close-quote, copying back over open-quote */
171 if(!*p || *p=='\n') {
172 fprintf(stderr, "moira: Unbalanced quotes in command line\n");
176 if(*++p!='"' && (*p<'0' || *p>'9') && (*p!='\\')) {
177 fprintf(stderr, "moira: Bad use of \\\n");
179 } else if (*p>='0' && *p<='9') {
180 num=(*p-'0')*64 + (*++p-'0')*8 + (*++p-'0');
186 if(p==d+1) {*d='\0'; p++;}
187 else while(p>=d) *p--=' ';
189 if(*p==' ' || *p=='\t') {
190 /* skip whitespace */
191 for(*p++='\0'; *p==' ' || *p=='\t'; p++);
192 if(*p && *p!='\n') argv[++argc]=p--;
195 if(*p=='\n') *p='\0';
201 int status = mr_noop();
202 if (status) com_err("moira (noop)", status, "");
207 sending_version_no = MR_VERSION_2;
212 sending_version_no = MR_VERSION_1;
215 test_connect(argc, argv)
225 status = mr_connect(server);
226 if (status) com_err("moira (connect)", status, "");
231 int status = mr_disconnect();
232 if (status) com_err("moira (disconnect)", status, "");
240 memset(host, 0, sizeof(host));
242 if (status = mr_host(host, sizeof(host) - 1))
243 com_err("moira (host)", status, "");
245 printf("You are connected to host %s\n", host);
248 test_auth(argc, argv)
254 status = mr_auth("mrtest");
255 if (status) com_err("moira (auth)", status, "");
258 test_script(argc, argv)
263 char input[BUFSIZ], *cp;
264 int status, oldstdout, oldstderr;
267 com_err("moira (script)", 0, "too many levels deep in script files\n");
272 com_err("moira (script)", 0, "Usage: script input_file [ output_file ]");
276 inp = fopen(argv[1], "r");
278 sprintf(input, "Cannot open input file %s", argv[1]);
279 com_err("moira (script)", 0, input);
284 printf("Redirecting output to %s\n", argv[2]);
288 status = open(argv[2], O_CREAT|O_WRONLY|O_APPEND, 0664);
293 sprintf(input, "Unable to redirect output to %s\n", argv[2]);
294 com_err("moira (script)", errno, input);
306 if (fgets(input, BUFSIZ, inp) == NULL)
308 if ((cp = strchr(input, '\n')) != (char *)NULL)
314 if (input[0] == '%') {
315 for (cp = &input[1]; *cp && isspace(*cp); cp++);
316 printf("Comment: %s\n", cp);
319 printf("Executing: %s\n", input);
338 print_reply(argc, argv)
343 for (i = 0; i < argc; i++) {
344 if (i != 0) printf(", ");
345 printf("%s", argv[i]);
352 test_query(argc, argv)
358 com_err("moira (query)", 0, "Usage: query handle [ args ... ]");
363 status = mr_query(argv[1], argc-2, argv+2, print_reply, (char *)NULL);
364 printf("%d tuple%s\n", count, ((count == 1) ? "" : "s"));
365 if (status) com_err("moira (query)", status, "");
368 test_access(argc, argv)
374 com_err("moira (access)", 0, "Usage: access handle [ args ... ]");
377 status = mr_access(argv[1], argc-2, argv+2);
378 if (status) com_err("moira (access)", status, "");
388 if (status = mr_do_update())
389 com_err("moira (dcm)", status, " while triggering dcm");
393 test_motd(argc, argv)
400 if (status = mr_motd(&motd))
401 com_err("moira (motd)", status, " while getting motd");
403 printf("%s\n", motd);
405 printf("No message of the day.\n");
410 printf("Available moira requests:\n");
412 printf("noop\t\t\tAsk Moira to do nothing\n");
413 printf("connect, c\t\tConnect to Moira server\n");
414 printf("disconnect, d\t\tDisconnect from server\n");
415 printf("host\t\t\tIdentify the server host\n");
416 printf("new, 2\t\t\tUse new protocol\n");
417 printf("old, 1\t\t\tUse old protocol\n");
418 printf("motd, m\t\t\tGet the Message of the Day\n");
419 printf("query, qy\t\tMake a query.\n");
420 printf("auth, a\t\t\tAuthenticate to Moira.\n");
421 printf("access\t\t\tCheck access to a Moira query.\n");
422 printf("dcm\t\t\tTrigger the DCM\n");
423 printf("script, s\t\tRead commands from a script.\n");
424 printf("list_requests, lr, ?\tList available commands.\n");
425 printf("quit, Q\t\t\tLeave the subsystem.\n");