]> andersk Git - moira.git/blob - clients/mrtest/mrtest.c
80ecc5ee9938a8305f9175ebf983a6281b23f2fc
[moira.git] / clients / mrtest / mrtest.c
1 /*
2  *      $Source$
3  *      $Author$
4  *      $Header$
5  *
6  *      Copyright (C) 1987 by the Massachusetts Institute of Technology
7  *      For copying and distribution information, please see the file
8  *      <mit-copyright.h>.
9  *
10  */
11
12 #ifndef lint
13 static char *rcsid_test_c = "$Header$";
14 #endif lint
15
16 #include <mit-copyright.h>
17 #include <stdio.h>
18 #include <sys/types.h>
19 #include <sys/file.h>
20 #include <fcntl.h>
21 #include <ctype.h>
22 #include <string.h>
23 #include <moira.h>
24 #include <com_err.h>
25
26 int recursion = 0;
27 extern int errno;
28 extern int sending_version_no;
29 int count, quit=0;
30 char *whoami;
31
32 #define MAXARGS 20
33
34 main(argc, argv)
35         int argc;
36         char **argv;
37 {       
38         int status;
39         char cmdbuf[BUFSIZ];
40         
41         whoami = argv[0];
42         
43         initialize_sms_error_table();
44         initialize_krb_error_table();
45
46         while(!quit) {
47                 printf("moira:  ");
48                 fflush(stdout);
49                 if(!fgets(cmdbuf,BUFSIZ,stdin)) break;
50                 execute_line(cmdbuf);
51         }
52         mr_disconnect();
53         exit(0);
54 }
55
56 execute_line(cmdbuf)
57      char *cmdbuf;
58 {
59   int argc;
60   char *argv[MAXARGS];
61
62   argc=parse(cmdbuf, argv);
63   if(argc==0) return;
64   if(!strcmp(argv[0],"noop"))
65     test_noop();
66   else if(!strcmp(argv[0],"connect") || !strcmp(argv[0],"c"))
67     test_connect(argc, argv);
68   else if(!strcmp(argv[0],"disconnect") || !strcmp(argv[0],"d"))
69     test_disconnect();
70   else if(!strcmp(argv[0],"host"))
71     test_host();
72   else if(!strcmp(argv[0],"new") || !strcmp(argv[0],"2"))
73     test_new();
74   else if(!strcmp(argv[0],"old") || !strcmp(argv[0],"1"))
75     test_old();
76   else if(!strcmp(argv[0],"motd"))
77     test_motd();
78   else if(!strcmp(argv[0],"query") || !strcmp(argv[0],"qy"))
79     test_query(argc, argv);
80   else if(!strcmp(argv[0],"auth") || !strcmp(argv[0],"a"))
81     test_auth(argc, argv);
82   else if(!strcmp(argv[0],"access"))
83     test_access(argc, argv);
84   else if(!strcmp(argv[0],"dcm"))
85     test_dcm();
86   else if(!strcmp(argv[0],"script") || !strcmp(argv[0],"s"))
87     test_script(argc, argv);
88   else if(!strcmp(argv[0],"list_requests") ||
89           !strcmp(argv[0],"lr") || !strcmp(argv[0],"?"))
90     test_list_requests();
91   else if(!strcmp(argv[0],"quit") || !strcmp(argv[0],"Q"))
92     quit=1;
93   else fprintf(stderr, "moira: Unknown request \"%s\"."
94                "Type \"?\" for a request list.\n", argv[0]);
95 }
96
97 int
98 parse(buf, argv)
99      char *buf, *argv[MAXARGS];
100 {
101   char *p;
102   int argc, num;
103         
104   for(p=buf, argc=0, argv[0]=buf; *p && *p!='\n'; p++) {
105     if(*p=='"') {
106       char *d=p++;
107       /* skip to close-quote, copying back over open-quote */
108       while(*p!='"') {
109         if(!*p || *p=='\n') {
110           fprintf(stderr, "moira: Unbalanced quotes in command line\n");
111           return 0;
112         }
113         if(*p=='\\') {
114           if(*++p!='"' && (*p<'0' || *p>'9')) {
115             fprintf(stderr, "moira: Bad use of \\\n");
116             return 0;
117           } else if (*p!='"') {
118             num=(*p-'0')*64 + (*++p-'0')*8 + (*++p-'0');
119             *p=num;
120           }
121         }
122         *d++=*p++;
123       }
124       if(p==d+1) {*d='\0'; p++;}
125       else while(p>=d) *p--=' ';
126     }
127     if(*p==' ' || *p=='\t') {
128       /* skip whitespace */
129       for(*p++='\0'; *p==' ' || *p=='\t'; p++);
130       if(*p && *p!='\n') argv[++argc]=p--;
131     }
132   }
133   if(*p=='\n') *p='\0';
134   return argc+1;
135 }
136
137 test_noop()
138 {
139         int status = mr_noop();
140         if (status) com_err("moira (noop)", status, "");
141 }
142
143 test_new()
144 {
145         sending_version_no = MR_VERSION_2;
146 }
147
148 test_old()
149 {
150         sending_version_no = MR_VERSION_1;
151 }
152
153 test_connect(argc, argv)
154 int argc;
155 char *argv[];
156 {
157         char *server = "";
158         int status;
159
160         if (argc > 1) {
161             server = argv[1];
162         }
163         status = mr_connect(server);
164         if (status) com_err("moira (connect)", status, "");
165 }
166
167 test_disconnect()
168 {
169         int status = mr_disconnect();
170         if (status) com_err("moira (disconnect)", status, "");
171 }
172
173 test_host()
174 {
175         char host[BUFSIZ];
176         int status;
177
178         memset(host, 0, sizeof(host));
179
180         if (status = mr_host(host, sizeof(host) - 1))
181             com_err("moira (host)", status, "");
182         else
183             printf("You are connected to host %s\n", host);
184 }
185
186 test_auth(argc, argv)
187 int argc;
188 char *argv[];
189 {
190         int status;
191
192         status = mr_auth("mrtest");
193         if (status) com_err("moira (auth)", status, "");
194 }
195
196 test_script(argc, argv)
197 int argc;
198 char *argv[];
199 {
200     FILE *inp;
201     char input[BUFSIZ], *cp;
202     int status, oldstdout, oldstderr;
203
204     if (recursion > 8) {
205         com_err("moira (script)", 0, "too many levels deep in script files\n");
206         return;
207     }
208
209     if (argc < 2) {
210         com_err("moira (script)", 0, "Usage: script input_file [ output_file ]");
211         return;
212     }
213
214     inp = fopen(argv[1], "r");
215     if (inp == NULL) {
216         sprintf(input, "Cannot open input file %s", argv[1]);
217         com_err("moira (script)", 0, input);
218         return;
219     }
220
221     if (argc == 3) {
222         printf("Redirecting output to %s\n", argv[2]);
223         fflush(stdout);
224         oldstdout = dup(1);
225         close(1);
226         status = open(argv[2], O_CREAT|O_WRONLY|O_APPEND, 0664);
227         if (status != 1) {
228             close(status);
229             dup2(oldstdout, 1);
230             argc = 2;
231             sprintf(input, "Unable to redirect output to %s\n", argv[2]);
232             com_err("moira (script)", errno, input);
233         } else {
234             fflush(stderr);
235             oldstderr = dup(2);
236             close(2);
237             dup2(1, 2);
238         }
239     }
240
241     recursion++;
242
243     for(;;) {
244         if (fgets(input, BUFSIZ, inp) == NULL)
245           break;
246         if ((cp = strchr(input, '\n')) != (char *)NULL)
247           *cp = 0;
248         if (input[0] == 0) {
249             printf("\n");
250             continue;
251         }
252         if (input[0] == '%') {
253             for (cp = &input[1]; *cp && isspace(*cp); cp++);
254             printf("Comment: %s\n", cp);
255             continue;
256         }
257         printf("Executing: %s\n", input);
258         execute_line(input);
259     }
260
261     recursion--;
262
263     fclose(inp);
264     if (argc == 3) {
265         fflush(stdout);
266         close(1);
267         dup2(oldstdout, 1);
268         close(oldstdout);
269         fflush(stderr);
270         close(2);
271         dup2(oldstderr, 2);
272         close(oldstderr);
273     }
274 }
275
276 print_reply(argc, argv)
277         int argc;
278         char **argv;
279 {
280         int i;
281         for (i = 0; i < argc; i++) {
282                 if (i != 0) printf(", ");
283                 printf("%s", argv[i]);
284         }
285         printf("\n");
286         count++;
287         return(MR_CONT);
288 }
289
290 test_query(argc, argv)
291         int argc;
292         char **argv;
293 {
294         int status;
295         if (argc < 2) {
296                 com_err("moira (query)", 0, "Usage: query handle [ args ... ]");
297                 return;
298         }
299
300         count = 0;
301         status = mr_query(argv[1], argc-2, argv+2, print_reply, (char *)NULL);
302         printf("%d tuple%s\n", count, ((count == 1) ? "" : "s"));
303         if (status) com_err("moira (query)", status, "");
304 }
305
306 test_access(argc, argv)
307         int argc;
308         char **argv;
309 {
310         int status;
311         if (argc < 2) {
312                 com_err("moira (access)", 0, "Usage: access handle [ args ... ]");
313                 return;
314         }
315         status = mr_access(argv[1], argc-2, argv+2);
316         if (status) com_err("moira (access)", status, "");
317 }
318
319
320 test_dcm(argc, argv)
321         int argc;
322         char **argv;
323 {
324         int status;
325
326         if (status = mr_do_update())
327           com_err("moira (dcm)", status, " while triggering dcm");
328 }
329
330
331 test_motd(argc, argv)
332         int argc;
333         char **argv;
334 {
335         int status;
336         char *motd;
337
338         if (status = mr_motd(&motd))
339           com_err("moira (motd)", status, " while getting motd");
340         if (motd)
341           printf("%s\n", motd);
342         else
343           printf("No message of the day.\n");
344 }
345
346 test_list_requests()
347 {
348         printf("Available moira requests:\n");
349         printf("\n");
350         printf("noop\t\t\tAsk Moira to do nothing\n");
351         printf("connect, c\t\tConnect to Moira server\n");
352         printf("disconnect, d\t\tDisconnect from server\n");
353         printf("host\t\t\tIdentify the server host\n");
354         printf("new, 2\t\t\tUse new protocol\n");
355         printf("old, 1\t\t\tUse old protocol\n");
356         printf("motd, m\t\t\tGet the Message of the Day\n");
357         printf("query, qy\t\tMake a query.\n");
358         printf("auth, a\t\t\tAuthenticate to Moira.\n");
359         printf("access\t\t\tCheck access to a Moira query.\n");
360         printf("dcm\t\t\tTrigger the DCM\n");
361         printf("script, s\t\tRead commands from a script.\n");
362         printf("list_requests, lr, ?\tList available commands.\n");
363         printf("quit, Q\t\t\tLeave the subsystem.\n");
364 }
This page took 0.745392 seconds and 3 git commands to generate.