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;
41 void discard_input(int, int, struct sigcontext *);
42 char *mr_gets(char *, char *, size_t);
51 struct sigaction action;
56 initialize_sms_error_table();
57 initialize_krb_error_table();
60 action.sa_handler = discard_input;
62 sigemptyset(&action.sa_mask);
63 sigaction(SIGINT, &action, NULL);
65 signal(SIGINT, discard_input);
70 if(!mr_gets("moira: ",cmdbuf,BUFSIZ)) break;
77 void discard_input(int sig, int code, struct sigcontext *scp)
83 char *mr_gets(char *prompt, char *buf, size_t len)
93 strncpy(buf, in, len-1);
100 printf("%s", prompt);
102 in=fgets(buf, len, stdin);
104 if(strchr(buf,'\n')) *(strchr(buf,'\n'))=0;
114 argc=parse(cmdbuf, argv);
116 if(!strcmp(argv[0],"noop"))
118 else if(!strcmp(argv[0],"connect") || !strcmp(argv[0],"c"))
119 test_connect(argc, argv);
120 else if(!strcmp(argv[0],"disconnect") || !strcmp(argv[0],"d"))
122 else if(!strcmp(argv[0],"host"))
124 else if(!strcmp(argv[0],"new") || !strcmp(argv[0],"2"))
126 else if(!strcmp(argv[0],"old") || !strcmp(argv[0],"1"))
128 else if(!strcmp(argv[0],"motd"))
130 else if(!strcmp(argv[0],"query") || !strcmp(argv[0],"qy"))
131 test_query(argc, argv);
132 else if(!strcmp(argv[0],"auth") || !strcmp(argv[0],"a"))
133 test_auth(argc, argv);
134 else if(!strcmp(argv[0],"access"))
135 test_access(argc, argv);
136 else if(!strcmp(argv[0],"dcm"))
138 else if(!strcmp(argv[0],"script") || !strcmp(argv[0],"s"))
139 test_script(argc, argv);
140 else if(!strcmp(argv[0],"list_requests") ||
141 !strcmp(argv[0],"lr") || !strcmp(argv[0],"?"))
142 test_list_requests();
143 else if(!strcmp(argv[0],"quit") || !strcmp(argv[0],"Q"))
145 else fprintf(stderr, "moira: Unknown request \"%s\". Type \"?\" for a request list.\n", argv[0]);
150 char *buf, *argv[MAXARGS];
157 for(p=buf, argc=0, argv[0]=buf; *p && *p!='\n'; p++) {
160 /* skip to close-quote, copying back over open-quote */
162 if(!*p || *p=='\n') {
163 fprintf(stderr, "moira: Unbalanced quotes in command line\n");
167 if(*++p!='"' && (*p<'0' || *p>'9') && (*p!='\\')) {
168 fprintf(stderr, "moira: Bad use of \\\n");
170 } else if (*p>='0' && *p<='9') {
171 num=(*p-'0')*64 + (*++p-'0')*8 + (*++p-'0');
177 if(p==d+1) {*d='\0'; p++;}
178 else while(p>=d) *p--=' ';
180 if(*p==' ' || *p=='\t') {
181 /* skip whitespace */
182 for(*p++='\0'; *p==' ' || *p=='\t'; p++);
183 if(*p && *p!='\n') argv[++argc]=p--;
186 if(*p=='\n') *p='\0';
192 int status = mr_noop();
193 if (status) com_err("moira (noop)", status, "");
198 sending_version_no = MR_VERSION_2;
203 sending_version_no = MR_VERSION_1;
206 test_connect(argc, argv)
216 status = mr_connect(server);
217 if (status) com_err("moira (connect)", status, "");
222 int status = mr_disconnect();
223 if (status) com_err("moira (disconnect)", status, "");
231 memset(host, 0, sizeof(host));
233 if (status = mr_host(host, sizeof(host) - 1))
234 com_err("moira (host)", status, "");
236 printf("You are connected to host %s\n", host);
239 test_auth(argc, argv)
245 status = mr_auth("mrtest");
246 if (status) com_err("moira (auth)", status, "");
249 test_script(argc, argv)
254 char input[BUFSIZ], *cp;
255 int status, oldstdout, oldstderr;
258 com_err("moira (script)", 0, "too many levels deep in script files\n");
263 com_err("moira (script)", 0, "Usage: script input_file [ output_file ]");
267 inp = fopen(argv[1], "r");
269 sprintf(input, "Cannot open input file %s", argv[1]);
270 com_err("moira (script)", 0, input);
275 printf("Redirecting output to %s\n", argv[2]);
279 status = open(argv[2], O_CREAT|O_WRONLY|O_APPEND, 0664);
284 sprintf(input, "Unable to redirect output to %s\n", argv[2]);
285 com_err("moira (script)", errno, input);
297 if (fgets(input, BUFSIZ, inp) == NULL)
299 if ((cp = strchr(input, '\n')) != (char *)NULL)
305 if (input[0] == '%') {
306 for (cp = &input[1]; *cp && isspace(*cp); cp++);
307 printf("Comment: %s\n", cp);
310 printf("Executing: %s\n", input);
329 print_reply(argc, argv)
334 for (i = 0; i < argc; i++) {
335 if (i != 0) printf(", ");
336 printf("%s", argv[i]);
343 test_query(argc, argv)
349 com_err("moira (query)", 0, "Usage: query handle [ args ... ]");
354 status = mr_query(argv[1], argc-2, argv+2, print_reply, (char *)NULL);
355 printf("%d tuple%s\n", count, ((count == 1) ? "" : "s"));
356 if (status) com_err("moira (query)", status, "");
359 test_access(argc, argv)
365 com_err("moira (access)", 0, "Usage: access handle [ args ... ]");
368 status = mr_access(argv[1], argc-2, argv+2);
369 if (status) com_err("moira (access)", status, "");
379 if (status = mr_do_update())
380 com_err("moira (dcm)", status, " while triggering dcm");
384 test_motd(argc, argv)
391 if (status = mr_motd(&motd))
392 com_err("moira (motd)", status, " while getting motd");
394 printf("%s\n", motd);
396 printf("No message of the day.\n");
401 printf("Available moira requests:\n");
403 printf("noop\t\t\tAsk Moira to do nothing\n");
404 printf("connect, c\t\tConnect to Moira server\n");
405 printf("disconnect, d\t\tDisconnect from server\n");
406 printf("host\t\t\tIdentify the server host\n");
407 printf("new, 2\t\t\tUse new protocol\n");
408 printf("old, 1\t\t\tUse old protocol\n");
409 printf("motd, m\t\t\tGet the Message of the Day\n");
410 printf("query, qy\t\tMake a query.\n");
411 printf("auth, a\t\t\tAuthenticate to Moira.\n");
412 printf("access\t\t\tCheck access to a Moira query.\n");
413 printf("dcm\t\t\tTrigger the DCM\n");
414 printf("script, s\t\tRead commands from a script.\n");
415 printf("list_requests, lr, ?\tList available commands.\n");
416 printf("quit, Q\t\t\tLeave the subsystem.\n");