Most of the functionality in place.
[idzebra-moved-to-github.git] / isam / issh.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <strings.h>
4 #include <assert.h>
5
6 #include <common.h>
7 #include <isam.h>
8
9 static ISAM is = 0;
10
11 int c_open(char *p);
12 int c_close(char *p);
13 int c_write(char *p);
14 int c_merge(char *p);
15 int c_rewind(char *p);
16 int c_read(char *p);
17 int c_exit(char *p);
18 int c_help(char *p);
19 int c_list(char *p);
20 int c_pos(char *p);
21
22 struct
23 {
24     char *word;
25     int (*fun)(char *p);
26 } cmdl[] =
27 {
28     {"open", c_open},
29     {"close", c_close},
30     {"write", c_write},
31     {"merge", c_merge},
32     {"rewind", c_rewind},
33     {"read", c_read},
34     {"exit", c_exit},
35     {"quit", c_exit},
36     {"help", c_help},
37     {"list", 0},
38     {"pos", c_pos},
39
40     {0,0}
41 };
42
43
44 int c_pos(char *p)
45 {
46 }
47
48 int c_open(char *p)
49 {
50     if (!*p)
51     {
52         printf("Usage: open name\n");
53         return -1;
54     }
55     if (!(is = is_open(p, 1)))
56         return -1;
57     return 0;
58 }
59
60 int c_close(char *p)
61 {
62     if (!is)
63     {
64         printf("Open first.\n");
65         return -1;
66     }
67     is_close(is);
68     return 0;
69 }
70
71 int c_write(char *p) {}
72
73 int c_merge(char *p)
74 {
75     char line[100], buf[1024], ch;
76     int pos = 0, num = 0;
77     int op, key;
78     int val;
79
80     if (!is)
81     {
82         printf("Open first.\n");
83         return -1;
84     }
85     if (!sscanf(p, "%d", &val))
86     {
87         printf("Usage: merge <address>\n");
88         return -1;
89     }
90     printf("Enter pairs of <operation> <key>. Terminate with '.'\n");
91     while (gets(line) && *line != '.')
92     {
93         if (sscanf(line, "%d %d", &op, &key) < 2)
94         {
95             printf("Error in input\n");
96             return -1;
97         }
98         ch = op;
99         *(buf + pos++) = ch;
100         memcpy(buf + pos, &key, sizeof(key));
101         pos += sizeof(key);
102         num++;
103     }
104     printf("is_merge(-->%d) returns: %d\n", val, is_merge(is, val, num , buf));
105     return 0;
106 }
107
108 int c_rewind(char *p)
109 {
110     if (!is)
111     {
112         printf("Open first.\n");
113         return -1;
114     }
115 }
116
117 int c_read(char *p) {}
118
119 int c_exit(char *p)
120 {
121     exit(0);
122 }
123
124 int c_help(char *p)
125 {
126     int i;
127
128     printf("ISSH: ISAM debugging shell.\n");
129     printf("Commands:\n");
130     for (i = 0; cmdl[i].word; i++)
131         printf("    %s %s\n", cmdl[i].word, cmdl[i].fun ? "": "UNDEFINED");
132     return 0;
133 }
134
135 int main()
136 {
137     char line[1024];
138     static char word[1024] = "help", arg[1024] = "";
139     int i;
140
141     log_init(LOG_ALL, "issh", 0);
142
143     assert(common_resource = res_open("testres"));
144
145
146     for (;;)
147     {
148         printf("ISSH> ");
149         fflush(stdout);
150         if (!gets(line))
151             return 0;
152         *arg = '\0';
153         if (*line && sscanf(line, "%s %[^;]", word, arg) < 1)
154             abort();
155         for (i = 0; cmdl[i].word; i++)
156             if (!strncmp(cmdl[i].word, word, strlen(word)))
157             {
158                 printf("%s\n", (*cmdl[i].fun)(arg) == 0 ? "OK" : "FAILED");
159                 break;
160             }
161         if (!cmdl[i].word)
162             (*c_help)("");
163     }
164 }