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