]> andersk Git - moira.git/blob - clients/mrtest/mrtest.c
ss.h is in /usr/athena/include/ss not /usr/athena/include
[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 <ss/ss.h>
25
26 int ss;
27 int recursion = 0;
28 extern ss_request_table moira_test;
29 extern int sending_version_no;
30 int count;
31
32 main(argc, argv)
33         int argc;
34         char **argv;
35 {       
36         int status;
37         char *whoami;
38         
39         whoami = argv[0];
40         
41         init_ss_err_tbl();
42         initialize_sms_error_table();
43         initialize_krb_error_table();
44
45         ss = ss_create_invocation("moira", "2.0", (char *)NULL,
46                                   &moira_test, &status);
47         if (status != 0) {
48                 com_err(whoami, status, "Unable to create invocation");
49                 exit(1);
50         }
51         if (argc > 1) {
52             argv++;
53             ss_execute_command(ss, argv);
54         }
55         ss_listen(ss, &status);
56         if (status != 0) {
57                 com_err(whoami, status, 0);
58                 exit(1);
59         }
60         exit(0);
61 }
62
63 test_noop()
64 {
65         int status = mr_noop();
66         if (status) ss_perror(ss, status, "");
67 }
68
69 test_new()
70 {
71         sending_version_no = MR_VERSION_2;
72 }
73
74 test_old()
75 {
76         sending_version_no = MR_VERSION_1;
77 }
78
79 test_connect(argc, argv)
80 int argc;
81 char *argv[];
82 {
83         char *server = "";
84         int status;
85
86         if (argc > 1) {
87             server = argv[1];
88         }
89         status = mr_connect(server);
90         if (status) ss_perror(ss, status, "");
91 }
92
93 test_disconnect()
94 {
95         int status = mr_disconnect();
96         if (status) ss_perror(ss, status, "");
97 }
98
99 test_host()
100 {
101         char host[BUFSIZ];
102         int status;
103
104         memset(host, 0, sizeof(host));
105
106         if (status = mr_host(host, sizeof(host) - 1))
107             ss_perror(ss, status, "");
108         else
109             printf("You are connected to host %s\n", host);
110 }
111
112 test_auth()
113 {
114         int status;
115
116         status = mr_auth("mrtest");
117         if (status) ss_perror(ss, status, "");
118 }
119
120 test_script(argc, argv)
121 int argc;
122 char *argv[];
123 {
124     FILE *inp;
125     char input[BUFSIZ], *cp;
126     int status, oldstdout, oldstderr;
127
128     if (recursion > 8) {
129         ss_perror(ss, 0, "too many levels deep in script files\n");
130         return;
131     }
132
133     if (argc < 2) {
134         ss_perror(ss, 0, "Usage: script input_file [ output_file ]");
135         return;
136     }
137
138     inp = fopen(argv[1], "r");
139     if (inp == NULL) {
140         sprintf(input, "Cannot open input file %s", argv[1]);
141         ss_perror(ss, 0, input);
142         return;
143     }
144
145     if (argc == 3) {
146         printf("Redirecting output to %s\n", argv[2]);
147         fflush(stdout);
148         oldstdout = dup(1);
149         close(1);
150         status = open(argv[2], O_CREAT|O_WRONLY|O_APPEND, 0664);
151         if (status != 1) {
152             close(status);
153             dup2(oldstdout, 1);
154             argc = 2;
155             sprintf(input, "Unable to redirect output to %s\n", argv[2]);
156             ss_perror(ss, errno, input);
157         } else {
158             fflush(stderr);
159             oldstderr = dup(2);
160             close(2);
161             dup2(1, 2);
162         }
163     }
164
165     recursion++;
166
167     for(;;) {
168         if (fgets(input, BUFSIZ, inp) == NULL)
169           break;
170         if ((cp = strchr(input, '\n')) != (char *)NULL)
171           *cp = 0;
172         if (input[0] == 0) {
173             printf("\n");
174             continue;
175         }
176         if (input[0] == '%') {
177             for (cp = &input[1]; *cp && isspace(*cp); cp++);
178             printf("Comment: %s\n", cp);
179             continue;
180         }
181         printf("Executing: %s\n", input);
182         ss_execute_line(ss, input, &status);
183         if (status == SS_ET_COMMAND_NOT_FOUND) {
184             printf("Bad command: %s\n", input);
185         }
186     }
187
188     recursion--;
189
190     fclose(inp);
191     if (argc == 3) {
192         fflush(stdout);
193         close(1);
194         dup2(oldstdout, 1);
195         close(oldstdout);
196         fflush(stderr);
197         close(2);
198         dup2(oldstderr, 2);
199         close(oldstderr);
200     }
201 }
202
203 char *concat(str1, str2)
204         char *str1, *str2;
205 {
206         char *rtn;
207         extern char *malloc();
208         
209         if (!str1) {
210                 int len = strlen(str2) + 1 ;
211                 rtn = malloc(len);
212                 memcpy(rtn, str2, len);
213         } else {
214                 int len1 = strlen(str1);
215                 int len2 = strlen(str2) + 1;
216                 rtn = malloc(len1+len2);
217                 memcpy(rtn, str1, len1);
218                 memcpy(rtn+len1, str2, len2);
219         }
220         return rtn;
221 }
222
223
224
225 print_reply(argc, argv)
226         int argc;
227         char **argv;
228 {
229         int i;
230         for (i = 0; i < argc; i++) {
231                 if (i != 0) printf(", ");
232                 printf("%s", argv[i]);
233         }
234         printf("\n");
235         count++;
236         return(MR_CONT);
237 }
238
239 test_query(argc, argv)
240         int argc;
241         char **argv;
242 {
243         int status;
244         if (argc < 2) {
245                 ss_perror(ss, 0, "Usage: query handle [ args ... ]");
246                 return;
247         }
248
249         count = 0;
250         status = mr_query(argv[1], argc-2, argv+2, print_reply, (char *)NULL);
251         printf("%d tuple%s\n", count, ((count == 1) ? "" : "s"));
252         if (status) ss_perror(ss, status, "");
253 }
254
255 test_access(argc, argv)
256         int argc;
257         char **argv;
258 {
259         int status;
260         if (argc < 2) {
261                 ss_perror(ss, 0, "Usage: access handle [ args ... ]");
262                 return;
263         }
264         status = mr_access(argv[1], argc-2, argv+2);
265         if (status) ss_perror(ss, status, "");
266 }
267
268
269 test_dcm(argc, argv)
270         int argc;
271         char **argv;
272 {
273         int status;
274
275         if (status = mr_do_update())
276           ss_perror(ss, status, " while triggering dcm");
277 }
278
279
280 test_motd(argc, argv)
281         int argc;
282         char **argv;
283 {
284         int status;
285         char *motd;
286
287         if (status = mr_motd(&motd))
288           ss_perror(ss, status, " while getting motd");
289         if (motd)
290           printf("%s\n", motd);
291         else
292           printf("No message of the day.\n");
293 }
This page took 0.059008 seconds and 5 git commands to generate.