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