Towards GPL
[idzebra-moved-to-github.git] / isam / issh.c
1 /* $Id: issh.c,v 1.6 2002-08-02 19:26:56 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
3    Index Data Aps
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <assert.h>
29
30 #include <isam.h>
31
32 static ISAM is = 0;
33
34 int c_open(char *p);
35 int c_close(char *p);
36 int c_write(char *p);
37 int c_merge(char *p);
38 int c_rewind(char *p);
39 int c_read(char *p);
40 int c_exit(char *p);
41 int c_help(char *p);
42 int c_list(char *p);
43 int c_pos(char *p);
44
45 struct
46 {
47     char *word;
48     int (*fun)(char *p);
49 } cmdl[] =
50 {
51     {"open", c_open},
52     {"close", c_close},
53     {"write", c_write},
54     {"merge", c_merge},
55     {"rewind", c_rewind},
56     {"read", c_read},
57     {"exit", c_exit},
58     {"quit", c_exit},
59     {"help", c_help},
60     {"list", 0},
61     {"pos", c_pos},
62
63     {0,0}
64 };
65
66
67 int c_pos(char *p)
68 {
69 }
70
71 int c_open(char *p)
72 {
73     if (!*p)
74     {
75         printf("Usage: open name\n");
76         return -1;
77     }
78     if (!(is = is_open(p, 1)))
79         return -1;
80     return 0;
81 }
82
83 int c_close(char *p)
84 {
85     if (!is)
86     {
87         printf("Open first.\n");
88         return -1;
89     }
90     is_close(is);
91     return 0;
92 }
93
94 int c_write(char *p) {}
95
96 int c_merge(char *p)
97 {
98     char line[100], buf[1024], ch;
99     int pos = 0, num = 0;
100     int op, key;
101     int val;
102
103     if (!is)
104     {
105         printf("Open first.\n");
106         return -1;
107     }
108     if (!sscanf(p, "%d", &val))
109     {
110         printf("Usage: merge <address>\n");
111         return -1;
112     }
113     printf("Enter pairs of <operation> <key>. Terminate with '.'\n");
114     while (gets(line) && *line != '.')
115     {
116         if (sscanf(line, "%d %d", &op, &key) < 2)
117         {
118             printf("Error in input\n");
119             return -1;
120         }
121         ch = op;
122         *(buf + pos++) = ch;
123         memcpy(buf + pos, &key, sizeof(key));
124         pos += sizeof(key);
125         num++;
126     }
127     printf("is_merge(-->%d) returns: %d\n", val, is_merge(is, val, num , buf));
128     return 0;
129 }
130
131 int c_rewind(char *p)
132 {
133     if (!is)
134     {
135         printf("Open first.\n");
136         return -1;
137     }
138 }
139
140 int c_read(char *p) {}
141
142 int c_exit(char *p)
143 {
144     exit(0);
145 }
146
147 int c_help(char *p)
148 {
149     int i;
150
151     printf("ISSH: ISAM debugging shell.\n");
152     printf("Commands:\n");
153     for (i = 0; cmdl[i].word; i++)
154         printf("    %s %s\n", cmdl[i].word, cmdl[i].fun ? "": "UNDEFINED");
155     return 0;
156 }
157
158 int main()
159 {
160     char line[1024];
161     static char word[1024] = "help", arg[1024] = "";
162     int i;
163
164     log_init(LOG_ALL, "issh", 0);
165
166     common_resource = res_open("testres");
167     assert(common_resource);
168
169     for (;;)
170     {
171         printf("ISSH> ");
172         fflush(stdout);
173         if (!gets(line))
174             return 0;
175         *arg = '\0';
176         if (*line && sscanf(line, "%s %[^;]", word, arg) < 1)
177             abort();
178         for (i = 0; cmdl[i].word; i++)
179             if (!strncmp(cmdl[i].word, word, strlen(word)))
180             {
181                 printf("%s\n", (*cmdl[i].fun)(arg) == 0 ? "OK" : "FAILED");
182                 break;
183             }
184         if (!cmdl[i].word)
185             (*c_help)("");
186     }
187 }