]> andersk Git - moira.git/blob - clients/mrtest/mrtest.c
1699444ca2c1e2e36e1ff0c61266978c603abb05
[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  *
8  *      $Log$
9  *      Revision 1.4  1988-01-21 17:57:51  mar
10  *      added capability to read from a script file, and put output in another file
11  *
12  * Revision 1.3  88/01/07  17:42:06  mar
13  * print "tuple" or "tuples" as is correct
14  * 
15  * Revision 1.2  87/08/22  23:45:10  wesommer
16  * Removed extra RCS headers.
17  * 
18  * Revision 1.1  87/08/22  18:31:59  wesommer
19  * Initial revision
20  * 
21  */
22
23 #ifndef lint
24 static char *rcsid_test_c = "$Header$";
25 #endif lint
26
27 #include <stdio.h>
28 #include <sys/file.h>
29 #include <ctype.h>
30 #include <sms.h>
31 #include <ss.h>
32
33 int ss;
34 int recursion = 0;
35 extern ss_request_table sms_test;
36
37 #ifndef __SABER__
38 main(argc, argv)
39         int argc;
40         char **argv;
41 #else __SABER__
42 sms()
43 #endif __SABER__
44 {       
45         int status;
46         char *whoami;
47         
48 #ifndef __SABER__
49         whoami = argv[0];
50 #else
51         whoami = "sms";
52 #endif __SABER__
53         
54         init_ss_err_tbl();
55         init_sms_err_tbl();
56         init_krb_err_tbl();
57
58         ss = ss_create_invocation("sms", "1.0", (char *)NULL,
59                                   &sms_test, &status);
60         if (status != 0) {
61                 com_err(whoami, status, "Unable to create invocation");
62                 exit(1);
63         }
64         ss_listen(ss, &status);
65         if (status != 0) {
66                 com_err(whoami, status, 0);
67                 exit(1);
68         }
69 }
70
71 test_noop()
72 {
73         int status = sms_noop();
74         if (status) ss_perror(ss, status, 0);
75 }
76
77 test_connect()
78 {
79         int status = sms_connect();
80         if (status) ss_perror(ss, status, 0);
81 }
82
83 test_disconnect()
84 {
85         int status = sms_disconnect();
86         if (status) ss_perror(ss, status, 0);
87 }
88
89 test_auth()
90 {
91         int status = sms_auth();
92         if (status) ss_perror(ss, status, 0);
93 }
94
95 test_script(argc, argv)
96 int argc;
97 char *argv[];
98 {
99     FILE *inp;
100     char input[BUFSIZ], *cp, *index();
101     int status, oldstdout, oldstderr;
102
103     if (recursion > 8) {
104         ss_perror(ss, 0, "too many levels deep in script files\n");
105         return;
106     }
107
108     if (argc < 2) {
109         ss_perror(ss, 0, "Usage: script input_file [ output_file ]");
110         return;
111     }
112
113     inp = fopen(argv[1], "r");
114     if (inp == NULL) {
115         ss_perror(ss, 0, "Cannot open input file %s", argv[1]);
116         return;
117     }
118
119     if (argc == 3) {
120         printf("Redirecting output to %s\n", argv[2]);
121         fflush(stdout);
122         oldstdout = dup(1);
123         close(1);
124         status = open(argv[2], O_CREAT|O_WRONLY|O_APPEND, 0664);
125         if (status != 1) {
126             close(status);
127             dup2(oldstdout, 1);
128             argc = 2;
129             ss_perror(ss, errno, "Unable to redirect output to %s\n", argv[2]);
130         } else {
131             fflush(stderr);
132             oldstderr = dup(2);
133             close(2);
134             dup2(1, 2);
135         }
136     }
137
138     recursion++;
139
140     for(;;) {
141         if (fgets(input, BUFSIZ, inp) == NULL)
142           break;
143         if ((cp = index(input, '\n')) != (char *)NULL)
144           *cp = 0;
145         if (input[0] == 0)
146           continue;
147         if (input[0] == '%') {
148             for (cp = &input[1]; *cp && isspace(*cp); cp++);
149             printf("Comment: %s\n", cp);
150             continue;
151         }
152         printf("Executing: %s\n", input);
153         ss_execute_line(ss, input, &status);
154         if (status == SS_ET_COMMAND_NOT_FOUND) {
155             printf("Bad command: %s\n", input);
156         }
157     }
158
159     recursion--;
160
161     fclose(inp);
162     if (argc == 3) {
163         fflush(stdout);
164         close(1);
165         dup2(oldstdout, 1);
166         close(oldstdout);
167         fflush(stderr);
168         close(2);
169         dup2(oldstderr, 2);
170         close(oldstderr);
171     }
172 }
173
174 char *concat(str1, str2)
175         char *str1, *str2;
176 {
177         char *rtn;
178         extern char *malloc();
179         
180         if (!str1) {
181                 int len = strlen(str2) + 1 ;
182                 rtn = malloc(len);
183                 bcopy(str2, rtn, len);
184         } else {
185                 int len1 = strlen(str1);
186                 int len2 = strlen(str2) + 1;
187                 rtn = malloc(len1+len2);
188                 bcopy(str1, rtn, len1);
189                 bcopy(str2, rtn+len1, len2);
190         }
191         return rtn;
192 }
193
194 test_shutdown(argc, argv)
195         int argc;
196         char **argv;
197 {
198         char *reason = NULL;
199         int status, i;
200         
201         if (argc < 2) {
202                 ss_perror(ss, 0, "Usage: shutdown reason ...");
203                 return;
204         }
205         
206         for (i = 1 ; i < argc; i++) {
207                 if (i != 1) reason = concat(reason, " ");
208                 reason = concat(reason, argv[i]);
209         }
210         status = sms_shutdown(reason);
211         if (status) ss_perror(ss, status, 0);
212 }
213 static int count;
214
215
216 print_reply(argc, argv)
217         int argc;
218         char **argv;
219 {
220         int i;
221         for (i = 0; i < argc; i++) {
222                 if (i != 0) printf(", ");
223                 printf("%s", argv[i]);
224         }
225         printf("\n");
226         count++;
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         count = 0;
239         status = sms_query(argv[1], argc-2, argv+2, print_reply, (char *)NULL);
240         printf("%d tuple%s\n", count, ((count == 1) ? "" : "s"));
241         if (status) ss_perror(ss, status, 0);
242 }
243
244 test_access(argc, argv)
245         int argc;
246         char **argv;
247 {
248         int status;
249         if (argc < 2) {
250                 ss_perror(ss, 0, "Usage: access handle [ args ... ]");
251                 return;
252         }
253         status = sms_access(argv[1], argc-2, argv+2);
254         if (status) ss_perror(ss, status, 0);
255 }
256
This page took 0.164557 seconds and 3 git commands to generate.