]> andersk Git - moira.git/blob - clients/mrtest/tst.c
2f85d2fd2faeaef0980bbfc49af3528a9e7a2af9
[moira.git] / clients / mrtest / tst.c
1 /*
2  *      $Source$
3  *      $Author$
4  *      $Header$
5  *
6  *      Copyright (C) 1991 by the Massachusetts Institute of Technology
7  *      For copying and distribution information, please see the file
8  *      <mit-copyright.h>.
9  *
10  */
11
12
13 #include <stdio.h>
14 #include <sys/types.h>
15 #include <sys/file.h>
16 #include <ctype.h>
17 #include <string.h>
18 #include <moira.h>
19 #include <ss/ss.h>
20 #include "mr_err_array.h"
21
22 extern ss_execute_line();
23 extern print_reply();
24 extern CompData();
25 extern int ss;
26 extern int errno;
27 extern int count;
28 extern int recursion;  
29
30 char *DataBuf;
31 char *ErrorBuf;
32
33 test_test (argc, argv)
34 int argc;
35 char *argv[];
36 {
37   FILE *inp, *outp;
38   char *cp;
39   int LineNum;
40   int status;
41   int NumArgs;
42   char *ValArgs[50];
43   char lastcmd[BUFSIZ], input[BUFSIZ], cmd[BUFSIZ];
44
45   DataBuf =  (char *)malloc (2222);
46   ErrorBuf = (char *)malloc (30);
47
48   if (recursion > 8) {
49     ss_perror(ss, 0, "too many levels deep in script/test files\n");
50     return;
51   }
52   
53   if (argc < 2) {
54     ss_perror(ss, 0, "Usage: test input_file [ output_file ]");
55     return;
56   }
57   
58   inp = fopen(argv[1], "r");
59   if (inp == NULL) {
60     sprintf(cmd, "Cannot open input file %s", argv[1]);
61     ss_perror(ss, 0, cmd);
62     return;
63   }
64   
65   if (argc == 3) {
66     outp = fopen(argv[2], "a");
67     if (!outp) {
68       sprintf(cmd, "Unable to open output for %s\n", argv[2]);   
69       ss_perror(ss, errno, cmd);
70       return;}}
71   else outp = stdout;
72
73   *lastcmd = '\0';
74
75   recursion++;
76   
77   for(LineNum = 0;;LineNum++) {
78     *DataBuf = '\0';
79     *ErrorBuf = '\0';
80     if (fgets(input, BUFSIZ, inp) == NULL) {
81       if (lastcmd[0] != '\0') {
82         strcpy (input, lastcmd);
83         lastcmd[0] = '\0';}
84       else break;}
85     if ((cp = strchr(input, '\n')) != (char *)NULL)
86       *cp = 0;
87     if (input[0] == 0) continue;
88
89     if (input[0] == '%') {
90       for (cp = &input[1]; *cp && isspace(*cp); cp++);
91       strcat(DataBuf, "Comment: ");
92       strcat(DataBuf, cp);
93       strcat(DataBuf, "\n");
94       continue;
95     }
96
97     if (input[0] == '>') {     /* Load in a Comparison String */
98       if (lastcmd[0] == '\0') {
99         fprintf(outp, 
100            "\nERROR IN LINE %d: Comparison String Without Comparable Command\n", 
101            LineNum);
102         fprintf(outp, "%s\n", input);
103         *DataBuf = '\0';
104         continue;}
105       else { /* Parse and Execute command with compare */
106         sprintf (cmd, "COMPARE_%s", lastcmd);
107         memset((char *)ValArgs, 0, sizeof(ValArgs));
108         Partial_parse_string(0, cmd, &NumArgs, ValArgs); 
109         ValArgs[NumArgs] = (char *)malloc(sizeof(char) * (1+strlen(input)));
110         strcpy(ValArgs[NumArgs], input);
111         status = ss_execute_command(ss, ValArgs); 
112         lastcmd[0] = '\0';
113
114 /* Dump errors and data if necessary */
115         if (!strcmp(ErrorBuf, "Malformed Comparison String0")) {
116           fprintf(outp, "\nERROR IN LINE %d: %s\n", LineNum, ErrorBuf);
117           fprintf(outp, "%s\n", input);
118           continue;}
119         else if (*ErrorBuf) { /* Data Error */
120           fprintf(outp, "\nERROR IN LINE %d: %s\n", LineNum, ErrorBuf);
121           fprintf(outp, "%s\n", DataBuf);
122           continue;}
123         else continue;  /* Command Checks */
124       }}
125
126 /* It wasn't a Comparison line, so clear the stack */
127     if (lastcmd[0] != '\0') {  /* Run an old command w/o a comparison string */
128       ss_execute_line(ss, lastcmd, &status);
129       if (status == SS_ET_COMMAND_NOT_FOUND) 
130         printf("Bad command: %s\n", input);
131       *lastcmd = '\0';}
132
133 /* Push command on the stack if it's comparable (currently only queries are) */
134     if (!(strncasecmp (input, "qy ", 3) && strncasecmp (input, "query ", 6))) {
135       /* Delay this command in case there's a comparison line following it */
136       strcpy (lastcmd, input);
137       continue;}
138
139 /* Non-comparible command; execute immediately */
140     ss_execute_line(ss, input, &status);
141     if (status == SS_ET_COMMAND_NOT_FOUND) {
142       printf("Bad command: %s\n", input);
143     }
144   }
145   
146   recursion--;
147   
148   fclose(inp);
149   if (argc > 2) 
150     close(outp);
151 }
152
153 /**********************************/
154
155 test_query_compare(argc, argv)
156      int argc;
157      char **argv;
158 { /* argv = "COMPARE_qy" + args to query + compstring */
159   int Qstatus = 0;      /* Status returned from the query */
160   char *CompTo[5];
161   int NumWordsComp=0, i;
162
163   if (argc < 2) { 
164     ss_perror(ss, 0, "Usage: query handle [ args ... ]");
165     return;
166   }
167 /* Execute query with a comparison string */
168   count = 0;
169
170   /* Parse comp string into '>', char, ErrMsg, NumEntries, and Data */
171   memset((char *)CompTo, 0, sizeof(CompTo));
172   Partial_parse_string (4, argv[argc-1], &NumWordsComp, CompTo);
173   if (NumWordsComp < 3) { /* Too few args in comparison string */
174         strcpy(ErrorBuf, "Malformed Comparison String1");}
175   else {
176
177     for (i=0;i<argc;i++) {
178       printf("argv[%d] = %s\n", i, argv[i]);}
179     printf("CompTo[3] = %s\n\n", CompTo[3]);
180
181
182     Qstatus = mr_query(argv[1], argc-3, argv+2, CompData,
183                        (char *)(&CompTo[3]));
184
185 /* Check the number of tuples returned */
186     if (*CompTo[2] == '<') {
187       if (isdigit(*(CompTo[2] + 1))) {
188         if (atoi(CompTo[2] + 1) <= count) 
189           strcat(ErrorBuf, "\nToo many tuples returned");}
190       else if (*ErrorBuf == '\0') 
191         strcpy(ErrorBuf, "Malformed Comparison String2");}
192     else if (*CompTo[2] == '>') {
193       if (isdigit(*(CompTo[2] + 1))) {
194         if (atoi(CompTo[2] + 1) >= count)
195           strcat(ErrorBuf, "\nToo few tuples returned");}
196       else if (*ErrorBuf == '\0') 
197         strcpy(ErrorBuf, "Malformed Comparison String3");}
198     else if (isdigit(*(CompTo[2]))) {
199       if (atoi(CompTo[2]) != count)
200         strcat(ErrorBuf, "\nWrong number tuples returned");}
201     else if (strcmp(CompTo[2], "*")) 
202       if (*ErrorBuf == '\0')
203         strcpy(ErrorBuf, "Malformed Comparison String4");
204     
205     /* Check return status */
206     if (!Comp_mr_err_table(Qstatus, CompTo[1]))
207       {
208         strcat(ErrorBuf, "\nRet Status Error, returns: ");
209         if (Qstatus)
210           strcat(ErrorBuf, MR_ERR_ARRAY[Qstatus - ERROR_TABLE_BASE_sms]);
211         else 
212           strcat(ErrorBuf, "SUCCESS");}
213     
214   }}
215
216 /********************************************/
217
218 int Comp_mr_err_table (ErrNum, ErrName)
219 int ErrNum;
220 char *ErrName;
221
222 /* Returns 1 if ErrNum = the string in ErrName in MR_ERR_ARRAY, else 0 */
223
224 {
225 if (!ErrNum && ((!strcasecmp(ErrName, "SUCCESS"))  
226                  || (!strcasecmp(ErrName, "S")))) 
227   return(1);
228 else if (!ErrNum) return (0);
229 else if (ErrNum >= ERROR_TABLE_BASE_sms) 
230   return (!strcmp (MR_ERR_ARRAY[ErrNum - ERROR_TABLE_BASE_sms], ErrName));
231 else return (0);
232 }
233
234 /********************************************/
235
236 int NumWords (Str)
237 char *Str;
238 {
239 int Count;
240 int CharIndex;
241
242 for (CharIndex = 0, Count = 0;*(Str + CharIndex);) {
243   if (isspace(*(Str + CharIndex)))
244     for (;isspace(*(Str + CharIndex));CharIndex++);
245   else if (*(Str + CharIndex) && !isspace(*(Str + CharIndex)))
246     for (Count++;(*(Str + CharIndex) && !isspace(*(Str + CharIndex)));
247          CharIndex++);
248 }
249 return(Count);
250 }
251
252 /**********************/
253
254 /*
255  * Partial_parse_string(MaxWords, Str, argc_ptr, argv_ptr)
256  *
257  * Function:
258  *      Parses line, dividing at whitespace, into tokens, returns
259  *      the "argc" and "argv" values, up to MaxWords-1 tokens.  Remaining
260  *      tokens after MaxWords-1 are all returned as one set in the final
261  *      slot of "argv".  If MaxWords = 0, the number of tokens is not limited.
262  * Arguments:
263  *      MaxWords (int)
264  *              Maximum number of tokens/strings to return
265  *      Str (char *)
266  *              Pointer to text string to be parsed.
267  *      argc_ptr (int *)
268  *              Where to put the "argc" (number of tokens) value.
269  *      argv_ptr (char *[])
270  *              Series of pointers to parsed tokens
271  */
272
273 Partial_parse_string (MaxWords, Str, argc_ptr, argv_ptr)
274 int MaxWords;
275 char *Str;
276 int *argc_ptr;
277 char *argv_ptr[];
278
279 {
280 char Buf[BUFSIZ];
281 int NumTokens;
282 int CharIndex;
283 int i;
284
285 for (CharIndex = 0, NumTokens = 0;*(Str + CharIndex) 
286      && ((NumTokens < MaxWords-1) || !MaxWords);) {
287   if (isspace(*(Str + CharIndex)))
288     for (;isspace(*(Str + CharIndex));CharIndex++);
289   else if (*(Str + CharIndex) && !isspace(*(Str + CharIndex)))
290     for (NumTokens++, i=0;(*(Str + CharIndex) && !isspace(*(Str + CharIndex)));
291          CharIndex++, i++) Buf[i] = *(Str + CharIndex);
292   Buf[i] = '\0';
293   argv_ptr[NumTokens-1] = (char *)malloc(sizeof(char) * (strlen(Buf) + 1));
294   strcpy(argv_ptr[NumTokens-1], Buf);
295 }
296 *argc_ptr = NumTokens;
297 if (NumTokens = MaxWords) {
298   argv_ptr[NumTokens-1]=
299     (char *) malloc(sizeof(char) * (1 + strlen(Str + CharIndex)));
300   strcpy (argv_ptr[NumTokens-1], (Str + CharIndex));}
301
302 }
303
This page took 0.048551 seconds and 3 git commands to generate.