]> andersk Git - moira.git/blame - clients/mrtest/tst.c
posixify source
[moira.git] / clients / mrtest / tst.c
CommitLineData
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>
4ca813bd 18#include <ss.h>
92e88c2c 19#include "mr_err_array.h"
20
21extern ss_execute_line();
22extern print_reply();
23extern CompData();
24extern int ss;
25extern int errno;
26extern int count;
27extern int recursion;
28
29char *DataBuf;
30char *ErrorBuf;
31
32test_test (argc, argv)
33int argc;
34char *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) {
bddd8492 59 sprintf(cmd, "Cannot open input file %s", argv[1]);
60 ss_perror(ss, 0, cmd);
92e88c2c 61 return;
62 }
63
64 if (argc == 3) {
65 outp = fopen(argv[2], "a");
66 if (!outp) {
bddd8492 67 sprintf(cmd, "Unable to open output for %s\n", argv[2]);
68 ss_perror(ss, errno, cmd);
92e88c2c 69 return;}}
70 else outp = stdout;
71
72 *lastcmd = '\0';
73
74 recursion++;
75
76 for(LineNum = 0;;LineNum++) {
77 *DataBuf = '\0';
78 *ErrorBuf = '\0';
79 if (fgets(input, BUFSIZ, inp) == NULL) {
80 if (lastcmd[0] != '\0') {
81 strcpy (input, lastcmd);
82 lastcmd[0] = '\0';}
83 else break;}
84 if ((cp = index(input, '\n')) != (char *)NULL)
85 *cp = 0;
86 if (input[0] == 0) continue;
87
88 if (input[0] == '%') {
89 for (cp = &input[1]; *cp && isspace(*cp); cp++);
90 strcat(DataBuf, "Comment: ");
91 strcat(DataBuf, cp);
92 strcat(DataBuf, "\n");
93 continue;
94 }
95
96 if (input[0] == '>') { /* Load in a Comparison String */
97 if (lastcmd[0] == '\0') {
98 fprintf(outp,
99 "\nERROR IN LINE %d: Comparison String Without Comparable Command\n",
100 LineNum);
101 fprintf(outp, "%s\n", input);
102 *DataBuf = '\0';
103 continue;}
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);
111 lastcmd[0] = '\0';
112
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);
117 continue;}
118 else if (*ErrorBuf) { /* Data Error */
119 fprintf(outp, "\nERROR IN LINE %d: %s\n", LineNum, ErrorBuf);
120 fprintf(outp, "%s\n", DataBuf);
121 continue;}
122 else continue; /* Command Checks */
123 }}
124
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);
130 *lastcmd = '\0';}
131
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);
136 continue;}
137
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);
142 }
143 }
144
145 recursion--;
146
147 fclose(inp);
148 if (argc > 2)
149 close(outp);
150}
151
152/**********************************/
153
154test_query_compare(argc, argv)
155 int argc;
156 char **argv;
157{ /* argv = "COMPARE_qy" + args to query + compstring */
158 int Qstatus = 0; /* Status returned from the query */
159 char *CompTo[5];
160 int NumWordsComp=0, i;
161
162 if (argc < 2) {
163 ss_perror(ss, 0, "Usage: query handle [ args ... ]");
164 return;
165 }
166/* Execute query with a comparison string */
167 count = 0;
168
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");}
174 else {
175
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]);
179
180
181 Qstatus = mr_query(argv[1], argc-3, argv+2, CompData,
182 (char *)(&CompTo[3]));
183
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");
203
204 /* Check return status */
205 if (!Comp_mr_err_table(Qstatus, CompTo[1]))
206 {
207 strcat(ErrorBuf, "\nRet Status Error, returns: ");
208 if (Qstatus)
209 strcat(ErrorBuf, MR_ERR_ARRAY[Qstatus - ERROR_TABLE_BASE_sms]);
210 else
211 strcat(ErrorBuf, "SUCCESS");}
212
213 }}
214
215/********************************************/
216
217int Comp_mr_err_table (ErrNum, ErrName)
218int ErrNum;
219char *ErrName;
220
221/* Returns 1 if ErrNum = the string in ErrName in MR_ERR_ARRAY, else 0 */
222
223{
224if (!ErrNum && ((!strcasecmp(ErrName, "SUCCESS"))
225 || (!strcasecmp(ErrName, "S"))))
226 return(1);
227else if (!ErrNum) return (0);
228else if (ErrNum >= ERROR_TABLE_BASE_sms)
229 return (!strcmp (MR_ERR_ARRAY[ErrNum - ERROR_TABLE_BASE_sms], ErrName));
230else return (0);
231}
232
233/********************************************/
234
235int NumWords (Str)
236char *Str;
237{
238int Count;
239int CharIndex;
240
241for (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)));
246 CharIndex++);
247}
248return(Count);
249}
250
251/**********************/
252
253/*
254 * Partial_parse_string(MaxWords, Str, argc_ptr, argv_ptr)
255 *
256 * Function:
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.
261 * Arguments:
262 * MaxWords (int)
263 * Maximum number of tokens/strings to return
264 * Str (char *)
265 * Pointer to text string to be parsed.
266 * argc_ptr (int *)
267 * Where to put the "argc" (number of tokens) value.
268 * argv_ptr (char *[])
269 * Series of pointers to parsed tokens
270 */
271
272Partial_parse_string (MaxWords, Str, argc_ptr, argv_ptr)
273int MaxWords;
274char *Str;
275int *argc_ptr;
276char *argv_ptr[];
277
278{
279char Buf[BUFSIZ];
280int NumTokens;
281int CharIndex;
282int i;
283
284for (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);
291 Buf[i] = '\0';
292 argv_ptr[NumTokens-1] = (char *)malloc(sizeof(char) * (strlen(Buf) + 1));
293 strcpy(argv_ptr[NumTokens-1], Buf);
294}
295*argc_ptr = NumTokens;
296if (NumTokens = MaxWords) {
297 argv_ptr[NumTokens-1]=
298 (char *) malloc(sizeof(char) * (1 + strlen(Str + CharIndex)));
299 strcpy (argv_ptr[NumTokens-1], (Str + CharIndex));}
300
301}
302
This page took 0.295513 seconds and 5 git commands to generate.