3 * Bare-bones Moira client
5 * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology
6 * For copying and distribution information, please see the file
10 #include <mit-copyright.h>
29 int recursion = 0, interactive;
30 int count, quit = 0, cancel = 0;
36 void discard_input(void);
37 char *mr_gets(char *prompt, char *buf, size_t len);
38 void execute_line(char *cmdbuf);
39 int parse(char *buf, char *argv[MAXARGS]);
40 int print_reply(int argc, char **argv, void *hint);
42 void test_connect(int argc, char **argv);
43 void test_disconnect(void);
46 void test_query(int argc, char **argv);
48 void test_access(int argc, char **argv);
50 void test_script(int argc, char **argv);
51 void test_list_requests(void);
53 int main(int argc, char **argv)
56 struct sigaction action;
59 interactive = (isatty(0) && isatty(1));
61 initialize_sms_error_table();
62 initialize_krb_error_table();
65 /* we don't want filename completion */
66 rl_bind_key('\t', rl_insert);
69 action.sa_handler = discard_input;
71 sigemptyset(&action.sa_mask);
72 sigaction(SIGINT, &action, NULL);
77 if (!mr_gets("moira: ", cmdbuf, BUFSIZ))
85 void discard_input(void)
89 /* if we're inside a script, we have to clean up file descriptors,
90 so don't jump out yet */
97 char *mr_gets(char *prompt, char *buf, size_t len)
103 in = readline(prompt);
109 strncpy(buf, in, len - 1);
116 printf("%s", prompt);
118 in = fgets(buf, len, stdin);
121 if (strchr(buf, '\n'))
122 *(strchr(buf, '\n')) = '\0';
126 void execute_line(char *cmdbuf)
131 argc = parse(cmdbuf, argv);
134 if (!strcmp(argv[0], "noop"))
136 else if (!strcmp(argv[0], "connect") || !strcmp(argv[0], "c"))
137 test_connect(argc, argv);
138 else if (!strcmp(argv[0], "disconnect") || !strcmp(argv[0], "d"))
140 else if (!strcmp(argv[0], "host"))
142 else if (!strcmp(argv[0], "motd"))
144 else if (!strcmp(argv[0], "query") || !strcmp(argv[0], "qy"))
145 test_query(argc, argv);
146 else if (!strcmp(argv[0], "auth") || !strcmp(argv[0], "a"))
148 else if (!strcmp(argv[0], "access"))
149 test_access(argc, argv);
150 else if (!strcmp(argv[0], "dcm"))
152 else if (!strcmp(argv[0], "script") || !strcmp(argv[0], "s"))
153 test_script(argc, argv);
154 else if (!strcmp(argv[0], "list_requests") ||
155 !strcmp(argv[0], "lr") || !strcmp(argv[0], "?"))
156 test_list_requests();
157 else if (!strcmp(argv[0], "quit") || !strcmp(argv[0], "Q"))
161 fprintf(stderr, "moira: Unknown request \"%s\". "
162 "Type \"?\" for a request list.\n", argv[0]);
166 int parse(char *buf, char *argv[MAXARGS])
174 for (p = buf, argc = 0, argv[0] = buf; *p && *p != '\n'; p++)
179 /* skip to close-quote, copying back over open-quote */
182 if (!*p || *p == '\n')
185 "moira: Unbalanced quotes in command line\n");
188 /* deal with \### or \\ */
191 if (*++p != '"' && (*p < '0' || *p > '9') && (*p != '\\'))
193 fprintf(stderr, "moira: Bad use of \\\n");
196 else if (*p >= '0' && *p <= '9')
198 num = (*p - '0') * 64 + (*++p - '0') * 8 + (*++p - '0');
215 if (*p == ' ' || *p == '\t')
217 /* skip whitespace */
218 for (*p++ = '\0'; *p == ' ' || *p == '\t'; p++)
220 if (*p && *p != '\n')
231 int status = mr_noop();
233 com_err("moira (noop)", status, "");
236 void test_connect(int argc, char *argv[])
243 status = mr_connect(server);
245 com_err("moira (connect)", status, "");
248 void test_disconnect(void)
250 int status = mr_disconnect();
252 com_err("moira (disconnect)", status, "");
260 memset(host, 0, sizeof(host));
262 if ((status = mr_host(host, sizeof(host) - 1)))
263 com_err("moira (host)", status, "");
265 printf("You are connected to host %s\n", host);
272 status = mr_auth("mrtest");
274 com_err("moira (auth)", status, "");
277 void test_script(int argc, char *argv[])
280 char input[BUFSIZ], *cp;
281 int status, oldstdout, oldstderr;
285 com_err("moira (script)", 0, "too many levels deep in script files\n");
291 com_err("moira (script)", 0, "Usage: script input_file [ output_file ]");
295 inp = fopen(argv[1], "r");
298 sprintf(input, "Cannot open input file %s", argv[1]);
299 com_err("moira (script)", 0, input);
305 printf("Redirecting output to %s\n", argv[2]);
309 status = open(argv[2], O_CREAT|O_WRONLY|O_APPEND, 0664);
315 sprintf(input, "Unable to redirect output to %s\n", argv[2]);
316 com_err("moira (script)", errno, input);
331 if (!fgets(input, BUFSIZ, inp))
333 if ((cp = strchr(input, '\n')))
342 for (cp = &input[1]; *cp && isspace(*cp); cp++)
344 printf("Comment: %s\n", cp);
347 printf("Executing: %s\n", input);
369 int print_reply(int argc, char **argv, void *hint)
372 for (i = 0; i < argc; i++)
376 printf("%s", argv[i]);
383 void test_query(int argc, char **argv)
390 com_err("moira (query)", 0, "Usage: query handle [ args ... ]");
395 /* Don't allow ^C during the query: it will confuse libmoira's
396 internal state. (Yay static variables) */
398 sigaddset(&sigs, SIGINT);
399 sigprocmask(SIG_BLOCK, &sigs, NULL);
400 status = mr_query(argv[1], argc - 2, argv + 2, print_reply, NULL);
401 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
402 printf("%d tuple%s\n", count, ((count == 1) ? "" : "s"));
404 com_err("moira (query)", status, "");
407 void test_access(int argc, char **argv)
412 com_err("moira (access)", 0, "Usage: access handle [ args ... ]");
415 status = mr_access(argv[1], argc - 2, argv + 2);
417 com_err("moira (access)", status, "");
424 if ((status = mr_do_update()))
425 com_err("moira (dcm)", status, " while triggering dcm");
433 if ((status = mr_motd(&motd)))
434 com_err("moira (motd)", status, " while getting motd");
436 printf("%s\n", motd);
438 printf("No message of the day.\n");
441 void test_list_requests(void)
443 printf("Available moira requests:\n");
445 printf("noop\t\t\tAsk Moira to do nothing\n");
446 printf("connect, c\t\tConnect to Moira server\n");
447 printf("disconnect, d\t\tDisconnect from server\n");
448 printf("host\t\t\tIdentify the server host\n");
449 printf("motd, m\t\t\tGet the Message of the Day\n");
450 printf("query, qy\t\tMake a query.\n");
451 printf("auth, a\t\t\tAuthenticate to Moira.\n");
452 printf("access\t\t\tCheck access to a Moira query.\n");
453 printf("dcm\t\t\tTrigger the DCM\n");
454 printf("script, s\t\tRead commands from a script.\n");
455 printf("list_requests, lr, ?\tList available commands.\n");
456 printf("quit, Q\t\t\tLeave the subsystem.\n");