6 * Copyright (C) 1991 by the Massachusetts Institute of Technology
7 * For copying and distribution information, please see the file
14 #include <sys/types.h>
20 #include "mr_err_array.h"
22 extern ss_execute_line();
33 test_test (argc, argv)
43 char lastcmd[BUFSIZ], input[BUFSIZ], cmd[BUFSIZ];
45 DataBuf = (char *)malloc (2222);
46 ErrorBuf = (char *)malloc (30);
49 ss_perror(ss, 0, "too many levels deep in script/test files\n");
54 ss_perror(ss, 0, "Usage: test input_file [ output_file ]");
58 inp = fopen(argv[1], "r");
60 sprintf(cmd, "Cannot open input file %s", argv[1]);
61 ss_perror(ss, 0, cmd);
66 outp = fopen(argv[2], "a");
68 sprintf(cmd, "Unable to open output for %s\n", argv[2]);
69 ss_perror(ss, errno, cmd);
77 for(LineNum = 0;;LineNum++) {
80 if (fgets(input, BUFSIZ, inp) == NULL) {
81 if (lastcmd[0] != '\0') {
82 strcpy (input, lastcmd);
85 if ((cp = strchr(input, '\n')) != (char *)NULL)
87 if (input[0] == 0) continue;
89 if (input[0] == '%') {
90 for (cp = &input[1]; *cp && isspace(*cp); cp++);
91 strcat(DataBuf, "Comment: ");
93 strcat(DataBuf, "\n");
97 if (input[0] == '>') { /* Load in a Comparison String */
98 if (lastcmd[0] == '\0') {
100 "\nERROR IN LINE %d: Comparison String Without Comparable Command\n",
102 fprintf(outp, "%s\n", input);
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);
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);
119 else if (*ErrorBuf) { /* Data Error */
120 fprintf(outp, "\nERROR IN LINE %d: %s\n", LineNum, ErrorBuf);
121 fprintf(outp, "%s\n", DataBuf);
123 else continue; /* Command Checks */
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);
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);
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);
153 /**********************************/
155 test_query_compare(argc, argv)
158 { /* argv = "COMPARE_qy" + args to query + compstring */
159 int Qstatus = 0; /* Status returned from the query */
161 int NumWordsComp=0, i;
164 ss_perror(ss, 0, "Usage: query handle [ args ... ]");
167 /* Execute query with a comparison string */
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");}
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]);
182 Qstatus = mr_query(argv[1], argc-3, argv+2, CompData,
183 (char *)(&CompTo[3]));
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");
205 /* Check return status */
206 if (!Comp_mr_err_table(Qstatus, CompTo[1]))
208 strcat(ErrorBuf, "\nRet Status Error, returns: ");
210 strcat(ErrorBuf, MR_ERR_ARRAY[Qstatus - ERROR_TABLE_BASE_sms]);
212 strcat(ErrorBuf, "SUCCESS");}
216 /********************************************/
218 int Comp_mr_err_table (ErrNum, ErrName)
222 /* Returns 1 if ErrNum = the string in ErrName in MR_ERR_ARRAY, else 0 */
225 if (!ErrNum && ((!strcasecmp(ErrName, "SUCCESS"))
226 || (!strcasecmp(ErrName, "S"))))
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));
234 /********************************************/
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)));
252 /**********************/
255 * Partial_parse_string(MaxWords, Str, argc_ptr, argv_ptr)
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.
264 * Maximum number of tokens/strings to return
266 * Pointer to text string to be parsed.
268 * Where to put the "argc" (number of tokens) value.
269 * argv_ptr (char *[])
270 * Series of pointers to parsed tokens
273 Partial_parse_string (MaxWords, Str, argc_ptr, argv_ptr)
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);
293 argv_ptr[NumTokens-1] = (char *)malloc(sizeof(char) * (strlen(Buf) + 1));
294 strcpy(argv_ptr[NumTokens-1], Buf);
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));}