]> andersk Git - moira.git/blob - clients/mrtest/mrtest.c
f4df4e417d36690fa92b687638f104e9755685c1
[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 <sms.h>
21 #include <ss.h>
22
23 int ss;
24 int recursion = 0;
25 extern ss_request_table sms_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 sms()
34 #endif __SABER__
35 {       
36         int status;
37         char *whoami;
38         
39 #ifndef __SABER__
40         whoami = argv[0];
41 #else
42         whoami = "sms";
43 #endif __SABER__
44         
45         init_ss_err_tbl();
46         init_sms_err_tbl();
47         init_krb_err_tbl();
48
49         ss = ss_create_invocation("sms", "2.0", (char *)NULL,
50                                   &sms_test, &status);
51         if (status != 0) {
52                 com_err(whoami, status, "Unable to create invocation");
53                 exit(1);
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 = sms_noop();
66         if (status) ss_perror(ss, status, 0);
67 }
68
69 test_new()
70 {
71         sending_version_no = SMS_VERSION_2;
72 }
73
74 test_old()
75 {
76         sending_version_no = SMS_VERSION_1;
77 }
78
79 test_connect(argc, argv)
80 int argc;
81 char *argv[];
82 {
83         char *server = "", serverbuf[256], *index();
84         int status;
85
86         if (argc > 1) {
87             server = argv[1];
88             if (index(server, ':') == NULL) {
89                 server = serverbuf;
90                 sprintf(serverbuf, "%s:%s", argv[1], "sms_db");
91             }
92         }
93         status = sms_connect(server);
94         if (status) ss_perror(ss, status, 0);
95 }
96
97 test_disconnect()
98 {
99         int status = sms_disconnect();
100         if (status) ss_perror(ss, status, 0);
101 }
102
103 test_auth()
104 {
105         int status;
106
107         status = sms_auth("smstest");
108         if (status) ss_perror(ss, status, 0);
109 }
110
111 test_script(argc, argv)
112 int argc;
113 char *argv[];
114 {
115     FILE *inp;
116     char input[BUFSIZ], *cp, *index();
117     int status, oldstdout, oldstderr;
118
119     if (recursion > 8) {
120         ss_perror(ss, 0, "too many levels deep in script files\n");
121         return;
122     }
123
124     if (argc < 2) {
125         ss_perror(ss, 0, "Usage: script input_file [ output_file ]");
126         return;
127     }
128
129     inp = fopen(argv[1], "r");
130     if (inp == NULL) {
131         ss_perror(ss, 0, "Cannot open input file %s", argv[1]);
132         return;
133     }
134
135     if (argc == 3) {
136         printf("Redirecting output to %s\n", argv[2]);
137         fflush(stdout);
138         oldstdout = dup(1);
139         close(1);
140         status = open(argv[2], O_CREAT|O_WRONLY|O_APPEND, 0664);
141         if (status != 1) {
142             close(status);
143             dup2(oldstdout, 1);
144             argc = 2;
145             ss_perror(ss, errno, "Unable to redirect output to %s\n", argv[2]);
146         } else {
147             fflush(stderr);
148             oldstderr = dup(2);
149             close(2);
150             dup2(1, 2);
151         }
152     }
153
154     recursion++;
155
156     for(;;) {
157         if (fgets(input, BUFSIZ, inp) == NULL)
158           break;
159         if ((cp = index(input, '\n')) != (char *)NULL)
160           *cp = 0;
161         if (input[0] == 0) {
162             printf("\n");
163             continue;
164         }
165         if (input[0] == '%') {
166             for (cp = &input[1]; *cp && isspace(*cp); cp++);
167             printf("Comment: %s\n", cp);
168             continue;
169         }
170         printf("Executing: %s\n", input);
171         ss_execute_line(ss, input, &status);
172         if (status == SS_ET_COMMAND_NOT_FOUND) {
173             printf("Bad command: %s\n", input);
174         }
175     }
176
177     recursion--;
178
179     fclose(inp);
180     if (argc == 3) {
181         fflush(stdout);
182         close(1);
183         dup2(oldstdout, 1);
184         close(oldstdout);
185         fflush(stderr);
186         close(2);
187         dup2(oldstderr, 2);
188         close(oldstderr);
189     }
190 }
191
192 char *concat(str1, str2)
193         char *str1, *str2;
194 {
195         char *rtn;
196         extern char *malloc();
197         
198         if (!str1) {
199                 int len = strlen(str2) + 1 ;
200                 rtn = malloc(len);
201                 bcopy(str2, rtn, len);
202         } else {
203                 int len1 = strlen(str1);
204                 int len2 = strlen(str2) + 1;
205                 rtn = malloc(len1+len2);
206                 bcopy(str1, rtn, len1);
207                 bcopy(str2, rtn+len1, len2);
208         }
209         return rtn;
210 }
211
212 static int count;
213
214
215 print_reply(argc, argv)
216         int argc;
217         char **argv;
218 {
219         int i;
220         for (i = 0; i < argc; i++) {
221                 if (i != 0) printf(", ");
222                 printf("%s", argv[i]);
223         }
224         printf("\n");
225         count++;
226         return(SMS_CONT);
227 }
228
229 test_query(argc, argv)
230         int argc;
231         char **argv;
232 {
233         int status;
234         if (argc < 2) {
235                 ss_perror(ss, 0, "Usage: query handle [ args ... ]");
236                 return;
237         }
238
239         count = 0;
240         status = sms_query(argv[1], argc-2, argv+2, print_reply, (char *)NULL);
241         printf("%d tuple%s\n", count, ((count == 1) ? "" : "s"));
242         if (status) ss_perror(ss, status, 0);
243 }
244
245 test_access(argc, argv)
246         int argc;
247         char **argv;
248 {
249         int status;
250         if (argc < 2) {
251                 ss_perror(ss, 0, "Usage: access handle [ args ... ]");
252                 return;
253         }
254         status = sms_access(argv[1], argc-2, argv+2);
255         if (status) ss_perror(ss, status, 0);
256 }
257
258
259 test_dcm(argc, argv)
260         int argc;
261         char **argv;
262 {
263         int status;
264
265         if (status = sms_do_update())
266           ss_perror(ss, status, " while triggering dcm");
267 }
This page took 0.041983 seconds and 3 git commands to generate.