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;
44 int main(int argc, char **argv)
47 struct sigaction action;
50 interactive = (isatty(0) && isatty(1));
52 initialize_sms_error_table();
53 initialize_krb_error_table();
56 /* we don't want filename completion */
57 rl_bind_key('\t', rl_insert);
60 action.sa_handler = discard_input;
62 sigemptyset(&action.sa_mask);
63 sigaction(SIGINT, &action, NULL);
68 if (!mr_gets("moira: ", cmdbuf, BUFSIZ))
76 void discard_input(void)
80 /* if we're inside a script, we have to clean up file descriptors,
81 so don't jump out yet */
88 char *mr_gets(char *prompt, char *buf, size_t len)
94 in = readline(prompt);
100 strncpy(buf, in, len - 1);
107 printf("%s", prompt);
109 in = fgets(buf, len, stdin);
112 if (strchr(buf, '\n'))
113 *(strchr(buf, '\n')) = '\0';
117 int execute_line(char *cmdbuf)
122 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"))
156 fprintf(stderr, "moira: Unknown request \"%s\". "
157 "Type \"?\" for a request list.\n", argv[0]);
161 int parse(char *buf, char *argv[MAXARGS])
169 for (p = buf, argc = 0, argv[0] = buf; *p && *p != '\n'; p++)
174 /* skip to close-quote, copying back over open-quote */
177 if (!*p || *p == '\n')
180 "moira: Unbalanced quotes in command line\n");
183 /* deal with \### or \\ */
186 if (*++p != '"' && (*p < '0' || *p > '9') && (*p != '\\'))
188 fprintf(stderr, "moira: Bad use of \\\n");
191 else if (*p >= '0' && *p <= '9')
193 num = (*p - '0') * 64 + (*++p - '0') * 8 + (*++p - '0');
210 if (*p == ' ' || *p == '\t')
212 /* skip whitespace */
213 for (*p++ = '\0'; *p == ' ' || *p == '\t'; p++)
215 if (*p && *p != '\n')
226 int status = mr_noop();
228 com_err("moira (noop)", status, "");
233 sending_version_no = MR_VERSION_2;
238 sending_version_no = MR_VERSION_1;
241 int test_connect(int argc, char *argv[])
248 status = mr_connect(server);
250 com_err("moira (connect)", status, "");
253 int test_disconnect(void)
255 int status = mr_disconnect();
257 com_err("moira (disconnect)", status, "");
265 memset(host, 0, sizeof(host));
267 if ((status = mr_host(host, sizeof(host) - 1)))
268 com_err("moira (host)", status, "");
270 printf("You are connected to host %s\n", host);
273 int test_auth(int argc, char *argv[])
277 status = mr_auth("mrtest");
279 com_err("moira (auth)", status, "");
282 int test_script(int argc, char *argv[])
285 char input[BUFSIZ], *cp;
286 int status, oldstdout, oldstderr;
290 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");
303 sprintf(input, "Cannot open input file %s", argv[1]);
304 com_err("moira (script)", 0, input);
310 printf("Redirecting output to %s\n", argv[2]);
314 status = open(argv[2], O_CREAT|O_WRONLY|O_APPEND, 0664);
320 sprintf(input, "Unable to redirect output to %s\n", argv[2]);
321 com_err("moira (script)", errno, input);
336 if (!fgets(input, BUFSIZ, inp))
338 if ((cp = strchr(input, '\n')))
347 for (cp = &input[1]; *cp && isspace(*cp); cp++)
349 printf("Comment: %s\n", cp);
352 printf("Executing: %s\n", input);
374 int print_reply(int argc, char **argv)
377 for (i = 0; i < argc; i++)
381 printf("%s", argv[i]);
388 int test_query(int argc, char **argv)
395 com_err("moira (query)", 0, "Usage: query handle [ args ... ]");
400 /* Don't allow ^C during the query: it will confuse libmoira's
401 internal state. (Yay static variables) */
403 sigaddset(&sigs, SIGINT);
404 sigprocmask(SIG_BLOCK, &sigs, NULL);
405 status = mr_query(argv[1], argc - 2, argv + 2, print_reply, NULL);
406 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
407 printf("%d tuple%s\n", count, ((count == 1) ? "" : "s"));
409 com_err("moira (query)", status, "");
412 int test_access(int argc, char **argv)
417 com_err("moira (access)", 0, "Usage: access handle [ args ... ]");
420 status = mr_access(argv[1], argc - 2, argv + 2);
422 com_err("moira (access)", status, "");
425 int test_dcm(int argc, char **argv)
429 if ((status = mr_do_update()))
430 com_err("moira (dcm)", status, " while triggering dcm");
433 int test_motd(int argc, char **argv)
438 if ((status = mr_motd(&motd)))
439 com_err("moira (motd)", status, " while getting motd");
441 printf("%s\n", motd);
443 printf("No message of the day.\n");
446 int test_list_requests(void)
448 printf("Available moira requests:\n");
450 printf("noop\t\t\tAsk Moira to do nothing\n");
451 printf("connect, c\t\tConnect to Moira server\n");
452 printf("disconnect, d\t\tDisconnect from server\n");
453 printf("host\t\t\tIdentify the server host\n");
454 printf("new, 2\t\t\tUse new protocol\n");
455 printf("old, 1\t\t\tUse old protocol\n");
456 printf("motd, m\t\t\tGet the Message of the Day\n");
457 printf("query, qy\t\tMake a query.\n");
458 printf("auth, a\t\t\tAuthenticate to Moira.\n");
459 printf("access\t\t\tCheck access to a Moira query.\n");
460 printf("dcm\t\t\tTrigger the DCM\n");
461 printf("script, s\t\tRead commands from a script.\n");
462 printf("list_requests, lr, ?\tList available commands.\n");
463 printf("quit, Q\t\t\tLeave the subsystem.\n");