]>
Commit | Line | Data |
---|---|---|
6e6374cb | 1 | /* |
2 | * $Source$ | |
3 | * $Author$ | |
4 | * $Header$ | |
5 | * | |
6 | * Copyright (C) 1987 by the Massachusetts Institute of Technology | |
c801de4c | 7 | * For copying and distribution information, please see the file |
8 | * <mit-copyright.h>. | |
6e6374cb | 9 | * |
6e6374cb | 10 | */ |
11 | ||
12 | #ifndef lint | |
13 | static char *rcsid_test_c = "$Header$"; | |
14 | #endif lint | |
15 | ||
c801de4c | 16 | #include <mit-copyright.h> |
6e6374cb | 17 | #include <stdio.h> |
39de499f | 18 | #include <sys/types.h> |
58b825a4 | 19 | #include <sys/file.h> |
a4fafd85 | 20 | #include <fcntl.h> |
58b825a4 | 21 | #include <ctype.h> |
f071d8a7 | 22 | #include <string.h> |
8defc06b | 23 | #include <moira.h> |
1a5c774c | 24 | #include <com_err.h> |
f281aa29 | 25 | #include <setjmp.h> |
26 | #include <signal.h> | |
27 | ||
28 | #ifdef USE_READLINE | |
29 | #include "readline.h" | |
30 | #endif | |
6e6374cb | 31 | |
58b825a4 | 32 | int recursion = 0; |
1a5c774c | 33 | extern int errno; |
59af7b90 | 34 | extern int sending_version_no; |
1a5c774c | 35 | int count, quit=0; |
36 | char *whoami; | |
f281aa29 | 37 | jmp_buf jb; |
1a5c774c | 38 | |
39 | #define MAXARGS 20 | |
6e6374cb | 40 | |
f281aa29 | 41 | void discard_input(int, int, struct sigcontext *); |
42 | char *mr_gets(char *, char *, size_t); | |
43 | ||
6e6374cb | 44 | main(argc, argv) |
45 | int argc; | |
46 | char **argv; | |
6e6374cb | 47 | { |
48 | int status; | |
1a5c774c | 49 | char cmdbuf[BUFSIZ]; |
f281aa29 | 50 | #ifdef POSIX |
51 | struct sigaction action; | |
52 | #endif | |
6e6374cb | 53 | |
6e6374cb | 54 | whoami = argv[0]; |
6e6374cb | 55 | |
365b5cce | 56 | initialize_sms_error_table(); |
57 | initialize_krb_error_table(); | |
6e6374cb | 58 | |
f281aa29 | 59 | #ifdef POSIX |
60 | action.sa_handler = discard_input; | |
61 | action.sa_flags = 0; | |
62 | sigemptyset(&action.sa_mask); | |
63 | sigaction(SIGINT, &action, NULL); | |
64 | #else | |
65 | signal(SIGINT, discard_input); | |
66 | #endif | |
67 | setjmp(jb); | |
68 | ||
1a5c774c | 69 | while(!quit) { |
f281aa29 | 70 | if(!mr_gets("moira: ",cmdbuf,BUFSIZ)) break; |
1a5c774c | 71 | execute_line(cmdbuf); |
6e6374cb | 72 | } |
1a5c774c | 73 | mr_disconnect(); |
74 | exit(0); | |
75 | } | |
76 | ||
f281aa29 | 77 | void discard_input(int sig, int code, struct sigcontext *scp) |
78 | { | |
79 | putc('\n', stdout); | |
80 | longjmp(jb, 1); | |
81 | } | |
82 | ||
83 | char *mr_gets(char *prompt, char *buf, size_t len) | |
84 | { | |
85 | char *in; | |
86 | #ifdef USE_READLINE | |
87 | if(isatty(0)) { | |
88 | in=readline(prompt); | |
89 | ||
90 | if (!in) return NULL; | |
91 | if (*in) { | |
92 | add_history(in); | |
93 | strncpy(buf, in, len-1); | |
94 | buf[len]=0; | |
95 | } | |
96 | ||
97 | return buf; | |
98 | } | |
99 | #endif | |
100 | printf("%s", prompt); | |
101 | fflush(stdout); | |
102 | in=fgets(buf, len, stdin); | |
103 | if(!in) return in; | |
104 | if(strchr(buf,'\n')) *(strchr(buf,'\n'))=0; | |
105 | return buf; | |
106 | } | |
107 | ||
1a5c774c | 108 | execute_line(cmdbuf) |
109 | char *cmdbuf; | |
110 | { | |
111 | int argc; | |
112 | char *argv[MAXARGS]; | |
113 | ||
114 | argc=parse(cmdbuf, argv); | |
115 | if(argc==0) return; | |
116 | if(!strcmp(argv[0],"noop")) | |
117 | test_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")) | |
121 | test_disconnect(); | |
122 | else if(!strcmp(argv[0],"host")) | |
123 | test_host(); | |
124 | else if(!strcmp(argv[0],"new") || !strcmp(argv[0],"2")) | |
125 | test_new(); | |
126 | else if(!strcmp(argv[0],"old") || !strcmp(argv[0],"1")) | |
127 | test_old(); | |
128 | else if(!strcmp(argv[0],"motd")) | |
129 | test_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")) | |
137 | test_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")) | |
144 | quit=1; | |
125f757c | 145 | else fprintf(stderr, "moira: Unknown request \"%s\". Type \"?\" for a request list.\n", argv[0]); |
1a5c774c | 146 | } |
147 | ||
148 | int | |
149 | parse(buf, argv) | |
150 | char *buf, *argv[MAXARGS]; | |
151 | { | |
152 | char *p; | |
153 | int argc, num; | |
f281aa29 | 154 | |
155 | if(!*buf) return 0; | |
156 | ||
1a5c774c | 157 | for(p=buf, argc=0, argv[0]=buf; *p && *p!='\n'; p++) { |
158 | if(*p=='"') { | |
159 | char *d=p++; | |
160 | /* skip to close-quote, copying back over open-quote */ | |
161 | while(*p!='"') { | |
162 | if(!*p || *p=='\n') { | |
163 | fprintf(stderr, "moira: Unbalanced quotes in command line\n"); | |
164 | return 0; | |
2c031a2a | 165 | } |
1a5c774c | 166 | if(*p=='\\') { |
f281aa29 | 167 | if(*++p!='"' && (*p<'0' || *p>'9') && (*p!='\\')) { |
1a5c774c | 168 | fprintf(stderr, "moira: Bad use of \\\n"); |
169 | return 0; | |
f281aa29 | 170 | } else if (*p>='0' && *p<='9') { |
1a5c774c | 171 | num=(*p-'0')*64 + (*++p-'0')*8 + (*++p-'0'); |
172 | *p=num; | |
173 | } | |
6e6374cb | 174 | } |
1a5c774c | 175 | *d++=*p++; |
176 | } | |
177 | if(p==d+1) {*d='\0'; p++;} | |
178 | else while(p>=d) *p--=' '; | |
179 | } | |
180 | if(*p==' ' || *p=='\t') { | |
181 | /* skip whitespace */ | |
182 | for(*p++='\0'; *p==' ' || *p=='\t'; p++); | |
183 | if(*p && *p!='\n') argv[++argc]=p--; | |
184 | } | |
185 | } | |
186 | if(*p=='\n') *p='\0'; | |
187 | return argc+1; | |
6e6374cb | 188 | } |
189 | ||
190 | test_noop() | |
191 | { | |
8defc06b | 192 | int status = mr_noop(); |
1a5c774c | 193 | if (status) com_err("moira (noop)", status, ""); |
6e6374cb | 194 | } |
195 | ||
59af7b90 | 196 | test_new() |
197 | { | |
8defc06b | 198 | sending_version_no = MR_VERSION_2; |
59af7b90 | 199 | } |
200 | ||
201 | test_old() | |
202 | { | |
8defc06b | 203 | sending_version_no = MR_VERSION_1; |
59af7b90 | 204 | } |
205 | ||
011e0a54 | 206 | test_connect(argc, argv) |
207 | int argc; | |
208 | char *argv[]; | |
6e6374cb | 209 | { |
f071d8a7 | 210 | char *server = ""; |
011e0a54 | 211 | int status; |
212 | ||
94551bbc | 213 | if (argc > 1) { |
214 | server = argv[1]; | |
94551bbc | 215 | } |
8defc06b | 216 | status = mr_connect(server); |
1a5c774c | 217 | if (status) com_err("moira (connect)", status, ""); |
6e6374cb | 218 | } |
219 | ||
220 | test_disconnect() | |
221 | { | |
8defc06b | 222 | int status = mr_disconnect(); |
1a5c774c | 223 | if (status) com_err("moira (disconnect)", status, ""); |
6e6374cb | 224 | } |
225 | ||
66848930 | 226 | test_host() |
227 | { | |
228 | char host[BUFSIZ]; | |
229 | int status; | |
230 | ||
f071d8a7 | 231 | memset(host, 0, sizeof(host)); |
66848930 | 232 | |
8defc06b | 233 | if (status = mr_host(host, sizeof(host) - 1)) |
1a5c774c | 234 | com_err("moira (host)", status, ""); |
66848930 | 235 | else |
236 | printf("You are connected to host %s\n", host); | |
237 | } | |
238 | ||
1a5c774c | 239 | test_auth(argc, argv) |
240 | int argc; | |
241 | char *argv[]; | |
6e6374cb | 242 | { |
59af7b90 | 243 | int status; |
244 | ||
8defc06b | 245 | status = mr_auth("mrtest"); |
1a5c774c | 246 | if (status) com_err("moira (auth)", status, ""); |
6e6374cb | 247 | } |
248 | ||
58b825a4 | 249 | test_script(argc, argv) |
250 | int argc; | |
251 | char *argv[]; | |
252 | { | |
253 | FILE *inp; | |
f071d8a7 | 254 | char input[BUFSIZ], *cp; |
58b825a4 | 255 | int status, oldstdout, oldstderr; |
256 | ||
257 | if (recursion > 8) { | |
1a5c774c | 258 | com_err("moira (script)", 0, "too many levels deep in script files\n"); |
58b825a4 | 259 | return; |
260 | } | |
261 | ||
262 | if (argc < 2) { | |
1a5c774c | 263 | com_err("moira (script)", 0, "Usage: script input_file [ output_file ]"); |
58b825a4 | 264 | return; |
265 | } | |
266 | ||
267 | inp = fopen(argv[1], "r"); | |
268 | if (inp == NULL) { | |
bddd8492 | 269 | sprintf(input, "Cannot open input file %s", argv[1]); |
1a5c774c | 270 | com_err("moira (script)", 0, input); |
58b825a4 | 271 | return; |
272 | } | |
273 | ||
274 | if (argc == 3) { | |
275 | printf("Redirecting output to %s\n", argv[2]); | |
276 | fflush(stdout); | |
277 | oldstdout = dup(1); | |
278 | close(1); | |
279 | status = open(argv[2], O_CREAT|O_WRONLY|O_APPEND, 0664); | |
280 | if (status != 1) { | |
281 | close(status); | |
282 | dup2(oldstdout, 1); | |
283 | argc = 2; | |
bddd8492 | 284 | sprintf(input, "Unable to redirect output to %s\n", argv[2]); |
1a5c774c | 285 | com_err("moira (script)", errno, input); |
58b825a4 | 286 | } else { |
287 | fflush(stderr); | |
288 | oldstderr = dup(2); | |
289 | close(2); | |
290 | dup2(1, 2); | |
291 | } | |
292 | } | |
293 | ||
294 | recursion++; | |
295 | ||
296 | for(;;) { | |
297 | if (fgets(input, BUFSIZ, inp) == NULL) | |
298 | break; | |
f071d8a7 | 299 | if ((cp = strchr(input, '\n')) != (char *)NULL) |
58b825a4 | 300 | *cp = 0; |
6a592314 | 301 | if (input[0] == 0) { |
302 | printf("\n"); | |
303 | continue; | |
304 | } | |
58b825a4 | 305 | if (input[0] == '%') { |
306 | for (cp = &input[1]; *cp && isspace(*cp); cp++); | |
307 | printf("Comment: %s\n", cp); | |
308 | continue; | |
309 | } | |
310 | printf("Executing: %s\n", input); | |
1a5c774c | 311 | execute_line(input); |
58b825a4 | 312 | } |
313 | ||
314 | recursion--; | |
315 | ||
316 | fclose(inp); | |
317 | if (argc == 3) { | |
318 | fflush(stdout); | |
319 | close(1); | |
320 | dup2(oldstdout, 1); | |
321 | close(oldstdout); | |
322 | fflush(stderr); | |
323 | close(2); | |
324 | dup2(oldstderr, 2); | |
325 | close(oldstderr); | |
326 | } | |
327 | } | |
328 | ||
6e6374cb | 329 | print_reply(argc, argv) |
330 | int argc; | |
331 | char **argv; | |
332 | { | |
333 | int i; | |
334 | for (i = 0; i < argc; i++) { | |
335 | if (i != 0) printf(", "); | |
336 | printf("%s", argv[i]); | |
337 | } | |
338 | printf("\n"); | |
339 | count++; | |
8defc06b | 340 | return(MR_CONT); |
6e6374cb | 341 | } |
342 | ||
343 | test_query(argc, argv) | |
344 | int argc; | |
345 | char **argv; | |
346 | { | |
347 | int status; | |
348 | if (argc < 2) { | |
1a5c774c | 349 | com_err("moira (query)", 0, "Usage: query handle [ args ... ]"); |
6e6374cb | 350 | return; |
351 | } | |
59af7b90 | 352 | |
6e6374cb | 353 | count = 0; |
8defc06b | 354 | status = mr_query(argv[1], argc-2, argv+2, print_reply, (char *)NULL); |
303cb414 | 355 | printf("%d tuple%s\n", count, ((count == 1) ? "" : "s")); |
1a5c774c | 356 | if (status) com_err("moira (query)", status, ""); |
6e6374cb | 357 | } |
358 | ||
359 | test_access(argc, argv) | |
360 | int argc; | |
361 | char **argv; | |
362 | { | |
363 | int status; | |
364 | if (argc < 2) { | |
1a5c774c | 365 | com_err("moira (access)", 0, "Usage: access handle [ args ... ]"); |
6e6374cb | 366 | return; |
367 | } | |
8defc06b | 368 | status = mr_access(argv[1], argc-2, argv+2); |
1a5c774c | 369 | if (status) com_err("moira (access)", status, ""); |
6e6374cb | 370 | } |
fc3ac28e | 371 | |
372 | ||
373 | test_dcm(argc, argv) | |
374 | int argc; | |
375 | char **argv; | |
376 | { | |
377 | int status; | |
378 | ||
8defc06b | 379 | if (status = mr_do_update()) |
1a5c774c | 380 | com_err("moira (dcm)", status, " while triggering dcm"); |
fc3ac28e | 381 | } |
093b57b1 | 382 | |
383 | ||
384 | test_motd(argc, argv) | |
385 | int argc; | |
386 | char **argv; | |
387 | { | |
388 | int status; | |
389 | char *motd; | |
390 | ||
8defc06b | 391 | if (status = mr_motd(&motd)) |
1a5c774c | 392 | com_err("moira (motd)", status, " while getting motd"); |
093b57b1 | 393 | if (motd) |
394 | printf("%s\n", motd); | |
395 | else | |
396 | printf("No message of the day.\n"); | |
397 | } | |
1a5c774c | 398 | |
399 | test_list_requests() | |
400 | { | |
401 | printf("Available moira requests:\n"); | |
402 | printf("\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"); | |
417 | } |