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>
19 #include "mr_err_array.h"
21 extern ss_execute_line();
32 test_test (argc, argv)
42 char lastcmd[BUFSIZ], input[BUFSIZ], cmd[BUFSIZ];
44 DataBuf = (char *)malloc (2222);
45 ErrorBuf = (char *)malloc (30);
48 ss_perror(ss, 0, "too many levels deep in script/test files\n");
53 ss_perror(ss, 0, "Usage: test input_file [ output_file ]");
57 inp = fopen(argv[1], "r");
59 sprintf(cmd, "Cannot open input file %s", argv[1]);
60 ss_perror(ss, 0, cmd);
65 outp = fopen(argv[2], "a");
67 sprintf(cmd, "Unable to open output for %s\n", argv[2]);
68 ss_perror(ss, errno, cmd);
76 for(LineNum = 0;;LineNum++) {
79 if (fgets(input, BUFSIZ, inp) == NULL) {
80 if (lastcmd[0] != '\0') {
81 strcpy (input, lastcmd);
84 if ((cp = index(input, '\n')) != (char *)NULL)
86 if (input[0] == 0) continue;
88 if (input[0] == '%') {
89 for (cp = &input[1]; *cp && isspace(*cp); cp++);
90 strcat(DataBuf, "Comment: ");
92 strcat(DataBuf, "\n");
96 if (input[0] == '>') { /* Load in a Comparison String */
97 if (lastcmd[0] == '\0') {
99 "\nERROR IN LINE %d: Comparison String Without Comparable Command\n",
101 fprintf(outp, "%s\n", input);
104 else { /* Parse and Execute command with compare */
105 sprintf (cmd, "COMPARE_%s", lastcmd);
106 bzero((char *)ValArgs, sizeof(ValArgs));
107 Partial_parse_string(0, cmd, &NumArgs, ValArgs);
108 ValArgs[NumArgs] = (char *)malloc(sizeof(char) * (1+strlen(input)));
109 strcpy(ValArgs[NumArgs], input);
110 status = ss_execute_command(ss, ValArgs);
113 /* Dump errors and data if necessary */
114 if (!strcmp(ErrorBuf, "Malformed Comparison String0")) {
115 fprintf(outp, "\nERROR IN LINE %d: %s\n", LineNum, ErrorBuf);
116 fprintf(outp, "%s\n", input);
118 else if (*ErrorBuf) { /* Data Error */
119 fprintf(outp, "\nERROR IN LINE %d: %s\n", LineNum, ErrorBuf);
120 fprintf(outp, "%s\n", DataBuf);
122 else continue; /* Command Checks */
125 /* It wasn't a Comparison line, so clear the stack */
126 if (lastcmd[0] != '\0') { /* Run an old command w/o a comparison string */
127 ss_execute_line(ss, lastcmd, &status);
128 if (status == SS_ET_COMMAND_NOT_FOUND)
129 printf("Bad command: %s\n", input);
132 /* Push command on the stack if it's comparable (currently only queries are) */
133 if (!(strncasecmp (input, "qy ", 3) && strncasecmp (input, "query ", 6))) {
134 /* Delay this command in case there's a comparison line following it */
135 strcpy (lastcmd, input);
138 /* Non-comparible command; execute immediately */
139 ss_execute_line(ss, input, &status);
140 if (status == SS_ET_COMMAND_NOT_FOUND) {
141 printf("Bad command: %s\n", input);
152 /**********************************/
154 test_query_compare(argc, argv)
157 { /* argv = "COMPARE_qy" + args to query + compstring */
158 int Qstatus = 0; /* Status returned from the query */
160 int NumWordsComp=0, i;
163 ss_perror(ss, 0, "Usage: query handle [ args ... ]");
166 /* Execute query with a comparison string */
169 /* Parse comp string into '>', char, ErrMsg, NumEntries, and Data */
170 bzero((char *)CompTo, sizeof(CompTo));
171 Partial_parse_string (4, argv[argc-1], &NumWordsComp, CompTo);
172 if (NumWordsComp < 3) { /* Too few args in comparison string */
173 strcpy(ErrorBuf, "Malformed Comparison String1");}
176 for (i=0;i<argc;i++) {
177 printf("argv[%d] = %s\n", i, argv[i]);}
178 printf("CompTo[3] = %s\n\n", CompTo[3]);
181 Qstatus = mr_query(argv[1], argc-3, argv+2, CompData,
182 (char *)(&CompTo[3]));
184 /* Check the number of tuples returned */
185 if (*CompTo[2] == '<') {
186 if (isdigit(*(CompTo[2] + 1))) {
187 if (atoi(CompTo[2] + 1) <= count)
188 strcat(ErrorBuf, "\nToo many tuples returned");}
189 else if (*ErrorBuf == '\0')
190 strcpy(ErrorBuf, "Malformed Comparison String2");}
191 else if (*CompTo[2] == '>') {
192 if (isdigit(*(CompTo[2] + 1))) {
193 if (atoi(CompTo[2] + 1) >= count)
194 strcat(ErrorBuf, "\nToo few tuples returned");}
195 else if (*ErrorBuf == '\0')
196 strcpy(ErrorBuf, "Malformed Comparison String3");}
197 else if (isdigit(*(CompTo[2]))) {
198 if (atoi(CompTo[2]) != count)
199 strcat(ErrorBuf, "\nWrong number tuples returned");}
200 else if (strcmp(CompTo[2], "*"))
201 if (*ErrorBuf == '\0')
202 strcpy(ErrorBuf, "Malformed Comparison String4");
204 /* Check return status */
205 if (!Comp_mr_err_table(Qstatus, CompTo[1]))
207 strcat(ErrorBuf, "\nRet Status Error, returns: ");
209 strcat(ErrorBuf, MR_ERR_ARRAY[Qstatus - ERROR_TABLE_BASE_sms]);
211 strcat(ErrorBuf, "SUCCESS");}
215 /********************************************/
217 int Comp_mr_err_table (ErrNum, ErrName)
221 /* Returns 1 if ErrNum = the string in ErrName in MR_ERR_ARRAY, else 0 */
224 if (!ErrNum && ((!strcasecmp(ErrName, "SUCCESS"))
225 || (!strcasecmp(ErrName, "S"))))
227 else if (!ErrNum) return (0);
228 else if (ErrNum >= ERROR_TABLE_BASE_sms)
229 return (!strcmp (MR_ERR_ARRAY[ErrNum - ERROR_TABLE_BASE_sms], ErrName));
233 /********************************************/
241 for (CharIndex = 0, Count = 0;*(Str + CharIndex);) {
242 if (isspace(*(Str + CharIndex)))
243 for (;isspace(*(Str + CharIndex));CharIndex++);
244 else if (*(Str + CharIndex) && !isspace(*(Str + CharIndex)))
245 for (Count++;(*(Str + CharIndex) && !isspace(*(Str + CharIndex)));
251 /**********************/
254 * Partial_parse_string(MaxWords, Str, argc_ptr, argv_ptr)
257 * Parses line, dividing at whitespace, into tokens, returns
258 * the "argc" and "argv" values, up to MaxWords-1 tokens. Remaining
259 * tokens after MaxWords-1 are all returned as one set in the final
260 * slot of "argv". If MaxWords = 0, the number of tokens is not limited.
263 * Maximum number of tokens/strings to return
265 * Pointer to text string to be parsed.
267 * Where to put the "argc" (number of tokens) value.
268 * argv_ptr (char *[])
269 * Series of pointers to parsed tokens
272 Partial_parse_string (MaxWords, Str, argc_ptr, argv_ptr)
284 for (CharIndex = 0, NumTokens = 0;*(Str + CharIndex)
285 && ((NumTokens < MaxWords-1) || !MaxWords);) {
286 if (isspace(*(Str + CharIndex)))
287 for (;isspace(*(Str + CharIndex));CharIndex++);
288 else if (*(Str + CharIndex) && !isspace(*(Str + CharIndex)))
289 for (NumTokens++, i=0;(*(Str + CharIndex) && !isspace(*(Str + CharIndex)));
290 CharIndex++, i++) Buf[i] = *(Str + CharIndex);
292 argv_ptr[NumTokens-1] = (char *)malloc(sizeof(char) * (strlen(Buf) + 1));
293 strcpy(argv_ptr[NumTokens-1], Buf);
295 *argc_ptr = NumTokens;
296 if (NumTokens = MaxWords) {
297 argv_ptr[NumTokens-1]=
298 (char *) malloc(sizeof(char) * (1 + strlen(Str + CharIndex)));
299 strcpy (argv_ptr[NumTokens-1], (Str + CharIndex));}