1 /* $Header$ */
2 #include "pool.h"
3 #include "pool_memory.h"
4 #include "parsenodes.h"
5 #include "gramparse.h"
6 #include "parser.h"
7 
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 
12 enum {
13 	TEST_CONTINUE,
14 	TEST_QUIT
15 };
16 
17 extern int GetDatabaseEncoding(void);
18 
19 int
command(const char * cmd)20 command(const char *cmd)
21 {
22 	char		name[1024], value[1024];
23 
24 	if (*cmd == 'q')
25 		return TEST_QUIT;
26 
27 	if (sscanf(cmd, "pset %s %s", name, value) == 2)
28 	{
29 		if (strcmp(name, "standard_conforming_strings") == 0)
30 		{
31 			parser_set_param(name, value);
32 			fprintf(stdout, "pset %d\n", standard_conforming_strings);
33 		}
34 		else if (strcmp(name, "server_version") == 0)
35 		{
36 			parser_set_param(name, value);
37 			fprintf(stdout, "pset %d\n", server_version_num);
38 		}
39 		else if (strcmp(name, "server_encoding") == 0)
40 		{
41 			parser_set_param(name, value);
42 			fprintf(stdout, "pset %d\n", GetDatabaseEncoding());
43 		}
44 		else
45 			fprintf(stdout, "ERROR: pset %s\n", name);
46 	}
47 	return TEST_CONTINUE;
48 }
49 
main(int argc,char ** argv)50 int main(int argc, char **argv)
51 {
52 	List *tree;
53 	ListCell *l;
54 	int		state = TEST_CONTINUE;
55 	int		notty = (!isatty(fileno(stdin)) || !isatty(fileno(stdout)));
56 	char	line[1024];
57 
58 	while (state != TEST_QUIT)
59 	{
60 		if (!notty)
61 			fprintf(stdout, "> ");
62 
63 		if (!fgets(line, 1024, stdin))
64 			break;
65 
66 		*(strchr(line, (int) '\n')) = '\0';
67 
68 		if (line[0] == '#' || line[0] == '\0')
69 			continue;
70 
71 		if (line[0] == '\\')
72 		{
73 			state = command(line + 1);
74 			continue;
75 		}
76 
77 		tree = raw_parser(line);
78 
79 		if (tree == NULL)
80 		{
81 			printf("syntax error: %s\n", line);
82 		}
83 		else
84 		{
85 			foreach(l, tree)
86 			{
87 				Node *node = (Node *) lfirst(l);
88 				printf("%s\n", nodeToString(node));
89 			}
90 		}
91 
92 		free_parser();
93 	}
94 
95 	return 0;
96 }
97