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